summaryrefslogtreecommitdiff
path: root/lib/supple/capi.c
blob: c0f8f48abfc6a146fc1f2c3f88fa7d774b9355fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* supple/lib/supple/capi.c
 *
 * Sandbox (for) Untrusted Procedure Partitioning (in) Lua Engine - Supple
 *
 * Useful C methods for Supple including the userdata partitioning.
 *
 * Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
 *
 * For licence terms, see COPYING
 *
 */

#include <lua.h>
#include <lauxlib.h>

#include <sys/types.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

/* The API exposed, increment on backward-compatible changes */
#define CAPI_API 1
/* The ABI exposed, increment on backward-incompatible changes */
#define CAPI_ABI 1

static int
supple_capi_explain(lua_State *L)
{
	int one_type;
	if (lua_gettop(L) != 2) {
		return luaL_error(L, "Expected 2 args got %d", lua_gettop(L));
	}
	one_type = lua_type(L, 1);
	if ((one_type != LUA_TTABLE) &&
	    (one_type != LUA_TFUNCTION) &&
	    (one_type != LUA_TUSERDATA)) {
		return luaL_error(L, 
			"Expected one of table, function or userdata.  " \
			"Got %s instead",
			lua_typename(L, one_type));
	}
	if (lua_type(L, 2) != LUA_TSTRING) {
		return luaL_error(L, "Expected string, got %s instead",
				  lua_typename(L, lua_type(L, 2)));
	}
	lua_newtable(L);
	lua_pushvalue(L, 2);
	lua_setfield(L, -2, "tag");
	lua_pushstring(L, lua_typename(L, one_type));
	lua_setfield(L, -2, "type");
	if (one_type != LUA_TFUNCTION) {
		/* Get the metatable */
		if (lua_getmetatable(L, 1) != 0) {
			int idx = 1;
			/* Prepare a methods table */
			lua_newtable(L);
			lua_pushnil(L); /* For iterating */
			while (lua_next(L, -3) != 0) { /* Iterate the metatable */
				lua_pop(L, 1); /* Don't need the value */
				if (lua_type(L, -1) != LUA_TSTRING) {
					/* Not a string key */
				} else if (strcmp(lua_tostring(L, -1),
						  "__mode") == 0) {
					/* mode key is ignored */
				} else {
					/* dup */
					lua_pushvalue(L, -1);
					/* And copy into methods */
					lua_rawseti(L, -3, idx++);
				}
				/* Key is left ready for lua_next() */
			}
			/* Stuff the methods table into the return */
			lua_setfield(L, -3, "methods");
			/* And pop the metatable */
			lua_pop(L, 1);
		}
	}
	/* And return the new table */
	return 1;
}

static int
supple_capi_proxy_tostring(lua_State *L)
{
	/* arg 1 is the object, upvalue 1 is the original type */
	const char *orig_type = lua_tostring(L, lua_upvalueindex(1));
	lua_pushfstring(L, "%s: %p", orig_type, lua_touserdata(L, 1));
	return 1;
}

static int
supple_capi_proxy_type(lua_State *L)
{
	/* arg 1 is the object, upvalue 1 is the original type */
	lua_pushvalue(L, lua_upvalueindex(1));
	return 1;
}

static int
supple_capi_new_proxy(lua_State *L)
{
	void *proxy_ptr;
	/* Input is a type string */
	if (lua_type(L, 1) != LUA_TSTRING) {
		return luaL_error(L, "Expected type string, got %s",
				  lua_typename(L, lua_type(L, 1)));
	}
	proxy_ptr = lua_newuserdata(L, 1);
	if (proxy_ptr == NULL) {
		return 0;
	}
	lua_newtable(L);
	/* typestr, proxyobj, metatable */
	/* First job is the tostring metamethod, we always add that */
	lua_pushvalue(L, 1);
	lua_pushcclosure(L, supple_capi_proxy_tostring, 1);
	lua_setfield(L, -2, "__tostring");
	/* next the __type metamethod for the lua level type call */
	lua_pushvalue(L, 1);
	lua_pushcclosure(L, supple_capi_proxy_type, 1);
	lua_setfield(L, -2, "__type");
	/* And now prevent anything poking in us later */
	lua_pushboolean(L, 1);
	lua_setfield(L, -2, "__metatable");

	/* Finally set the metatable and return proxy, metatable */
	lua_pushvalue(L, -1);
	lua_setmetatable(L, -3);

	return 2;
}

/* Replacement for lbaselib.c's type method, honouring __type */
static int
supple_capi_type(lua_State *L)
{
	lua_settop(L, 1);
	if (lua_getmetatable(L, 1) != 0) {
		/* There is a metatable, try and find __type */
		lua_getfield(L, 2, "__type");
		if (lua_isfunction(L, 3)) {
			lua_pushvalue(L, 1);
			lua_call(L, 1, 1);
			return 1;
		}
	}
	
	lua_pushstring(L, lua_typename(L, lua_type(L, 1)));

	return 1;
}

/* Reimplementation of base type in case it's not available */
static int
supple_capi_rawtype(lua_State *L)
{
	lua_settop(L, 1);
	lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
	return 1;
}

/* Lockdown function, takes nothing, returns a string token and errno which
 * explains the result.  We're using SUPPLE_MKDTEMP as a template for
 * mkdtemp().  The wrapper ensures it is unset unless testing.
 */
#define TEMPDIR_NAME ((getenv("SUPPLE_MKDTEMP") != NULL) ? \
		      (getenv("SUPPLE_MKDTEMP")) : ("/var/tmp/suppleXXXXXX"))
static int
supple_capi_lockdown(lua_State *L)
{
	bool rootly = (geteuid() == 0);
	char *tempdir;
	lua_settop(L, 0); /* Discard any arguments */
	
	/* Prepare a copy of the tempdir template ready */
	tempdir = lua_newuserdata(L, strlen(TEMPDIR_NAME));
	strcpy(tempdir, TEMPDIR_NAME);
	/* And make a temp directory to use */
	if (mkdtemp(tempdir) != tempdir) {
		/* Failed to make the temp dir, give up */
		int saved_errno = errno;
		lua_pushliteral(L, "mkdir");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	/* Temporary directory made, if we're rootly, chown it */
	if (rootly) {
		if (chown(tempdir, 0, 0) != 0) {
			int saved_errno = errno;
			rmdir(tempdir);
			lua_pushliteral(L, "chown");
			lua_pushnumber(L, saved_errno);
			return 2;
		}
	}
	
	/* Change into that directory */
	if (chdir(tempdir) != 0) {
		int saved_errno = errno;
		rmdir(tempdir);
		lua_pushliteral(L, "chdir");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	/* Remove the directory */
	if (rmdir(tempdir) != 0) {
		int saved_errno = errno;
		lua_pushliteral(L, "rmdir");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	/* chroot() to the ephemeral dir */
	if (rootly && (chroot(".") != 0)) {
		int saved_errno = errno;
		lua_pushliteral(L, "chroot");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	/* Ensure our PWD is "/" */
	if (rootly && chdir("/") != 0) {
		int saved_errno = errno;
		lua_pushliteral(L, "chdir");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	/* Drop privs */
	if (rootly && setuid(getuid()) != 0) {
		int saved_errno = errno;
		lua_pushliteral(L, "setuid");
		lua_pushnumber(L, saved_errno);
		return 2;
	}
	
	lua_pushstring(L, (rootly ? "ok" : "oknonroot"));
	lua_pushnumber(L, 0);
	return 2;
}

static const struct luaL_Reg
supple_capi_functions[] = {
	{ "explain", supple_capi_explain },
	{ "new_proxy", supple_capi_new_proxy },
	{ "type", supple_capi_type },
	{ "rawtype", supple_capi_rawtype },
	{ "lockdown", supple_capi_lockdown },
	{ NULL, NULL }
};

int
luaopen_supple_capi(lua_State *L)
{
	luaL_register(L, "supple.capi", supple_capi_functions);
	lua_pushnumber(L, CAPI_API);
	lua_setfield(L, -2, "_API");
	lua_pushnumber(L, CAPI_ABI);
	lua_setfield(L, -2, "_ABI");
	return 1;
}