xref: /freebsd/sys/contrib/openzfs/module/lua/README.zfs (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy#
2*eda14cbcSMatt Macy# CDDL HEADER START
3*eda14cbcSMatt Macy#
4*eda14cbcSMatt Macy# This file and its contents are supplied under the terms of the
5*eda14cbcSMatt Macy# Common Development and Distribution License ("CDDL"), version 1.0.
6*eda14cbcSMatt Macy# You may only use this file in accordance with the terms of version
7*eda14cbcSMatt Macy# 1.0 of the CDDL.
8*eda14cbcSMatt Macy#
9*eda14cbcSMatt Macy# A full copy of the text of the CDDL should have accompanied this
10*eda14cbcSMatt Macy# source.  A copy of the CDDL is also available via the Internet at
11*eda14cbcSMatt Macy# http://www.illumos.org/license/CDDL.
12*eda14cbcSMatt Macy#
13*eda14cbcSMatt Macy# CDDL HEADER END
14*eda14cbcSMatt Macy#
15*eda14cbcSMatt Macy
16*eda14cbcSMatt Macy#
17*eda14cbcSMatt Macy# Copyright (c) 2017 by Delphix. All rights reserved.
18*eda14cbcSMatt Macy#
19*eda14cbcSMatt Macy
20*eda14cbcSMatt MacyIntroduction
21*eda14cbcSMatt Macy------------
22*eda14cbcSMatt Macy
23*eda14cbcSMatt MacyThis README describes the Lua interpreter source code that lives in the ZFS
24*eda14cbcSMatt Macysource tree to enable execution of ZFS channel programs, including its
25*eda14cbcSMatt Macymaintenance policy, the modifications that have been made to it, and how it
26*eda14cbcSMatt Macyshould (and should not) be used.
27*eda14cbcSMatt Macy
28*eda14cbcSMatt MacyFor a description of the Lua language and features exposed by ZFS channel
29*eda14cbcSMatt Macyprograms, please refer to the zfs-program(1m) man page instead.
30*eda14cbcSMatt Macy
31*eda14cbcSMatt Macy
32*eda14cbcSMatt MacyMaintenance policy
33*eda14cbcSMatt Macy------------------
34*eda14cbcSMatt Macy
35*eda14cbcSMatt MacyThe Lua runtime is considered stable software. Channel programs don't need much
36*eda14cbcSMatt Macycomplicated logic, so updates to the Lua runtime from upstream are viewed as
37*eda14cbcSMatt Macynice-to-have, but not required for channel programs to be well-supported. As
38*eda14cbcSMatt Macysuch, the Lua runtime in ZFS should be updated on an as-needed basis for
39*eda14cbcSMatt Macysecurity vulnerabilities, but not much else.
40*eda14cbcSMatt Macy
41*eda14cbcSMatt Macy
42*eda14cbcSMatt MacyModifications to Lua
43*eda14cbcSMatt Macy--------------------
44*eda14cbcSMatt Macy
45*eda14cbcSMatt MacyThe version of the Lua runtime we're using in ZFS has been modified in a variety
46*eda14cbcSMatt Macyof ways to make it more useful for the specific purpose of running channel
47*eda14cbcSMatt Macyprograms. These changes include:
48*eda14cbcSMatt Macy
49*eda14cbcSMatt Macy1. "Normal" Lua uses floating point for all numbers it stores, but those aren't
50*eda14cbcSMatt Macy   useful inside ZFS / the kernel. We have changed the runtime to use int64_t
51*eda14cbcSMatt Macy   throughout for all numbers.
52*eda14cbcSMatt Macy2. Some of the Lua standard libraries do file I/O or spawn processes, but
53*eda14cbcSMatt Macy   neither of these make sense from inside channel programs. We have removed
54*eda14cbcSMatt Macy   those libraries rather than reimplementing them using kernel APIs.
55*eda14cbcSMatt Macy3. The "normal" Lua runtime handles errors by failing fatally, but since this
56*eda14cbcSMatt Macy   version of Lua runs inside the kernel we must handle these failures and
57*eda14cbcSMatt Macy   return meaningful error codes to userland. We have customized the Lua
58*eda14cbcSMatt Macy   failure paths so that they aren't fatal.
59*eda14cbcSMatt Macy4. Running poorly-vetted code inside the kernel is always a risk; even if the
60*eda14cbcSMatt Macy   ability to do so is restricted to the root user, it's still possible to write
61*eda14cbcSMatt Macy   an incorrect program that results in an infinite loop or massive memory use.
62*eda14cbcSMatt Macy   We've added new protections into the Lua interpreter to limit the runtime
63*eda14cbcSMatt Macy   (measured in number of Lua instructions run) and memory overhead of running
64*eda14cbcSMatt Macy   a channel program.
65*eda14cbcSMatt Macy5. The Lua bytecode is not designed to be secure / safe, so it would be easy to
66*eda14cbcSMatt Macy   pass invalid bytecode which can panic the kernel. By comparison, the parser
67*eda14cbcSMatt Macy   is hardened and fails gracefully on invalid input. Therefore, we only accept
68*eda14cbcSMatt Macy   Lua source code at the ioctl level and then interpret it inside the kernel.
69*eda14cbcSMatt Macy
70*eda14cbcSMatt MacyEach of these modifications have been tested in the zfs-test suite. If / when
71*eda14cbcSMatt Macynew modifications are made, new tests should be added to the suite located in
72*eda14cbcSMatt Macyzfs-tests/tests/functional/channel_program/lua_core.
73*eda14cbcSMatt Macy
74*eda14cbcSMatt Macy
75*eda14cbcSMatt MacyHow to use this Lua interpreter
76*eda14cbcSMatt Macy-------------------------------
77*eda14cbcSMatt Macy
78*eda14cbcSMatt MacyFrom the above, it should be clear that this is not a general-purpose Lua
79*eda14cbcSMatt Macyinterpreter. Additional work would be required to extricate this custom version
80*eda14cbcSMatt Macyof Lua from ZFS and make it usable by other areas of the kernel.
81