-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.c
262 lines (220 loc) · 5.2 KB
/
session.c
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
#include "config.h"
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sched.h>
#ifndef MS_REC /* May not be defined in older glibc headers */
#define MS_REC 16384
#endif
#ifndef MS_SHARED /* May not be defined in older glibc headers */
#define MS_SHARED (1<<20) /* change to shared */
#endif
static const char *
get_homedir (void)
{
const char *home;
home = getenv ("HOME");
if (home == NULL)
{
/* try from the user database */
struct passwd *user = getpwuid (getuid());
if (user != NULL)
home = user->pw_dir;
}
return home;
}
static char *
build_path (const char *dir, const char *file)
{
char *path;
path = malloc (strlen (dir) + 1 + strlen (file) + 1);
if (path != NULL)
{
strcpy (path, dir);
strcat (path, "/");
strcat (path, file);
}
return path;
}
static void
make_symlink (const char *dest_dir, const char *dest_file,
const char *src_dir, const char *src_file)
{
char *d, *s;
d = build_path (dest_dir, dest_file);
if (d)
{
s = build_path (src_dir, src_file);
if (s)
{
if (symlink (s, d) == -1 && errno != EEXIST)
perror ("Failed to symlink");
free (s);
}
free (d);
}
}
static int
mount_rprivate (const char *path)
{
return mount (path, path, NULL, MS_PRIVATE|MS_REC, NULL);
}
static int
mount_rshared (const char *path)
{
return mount (path, path, NULL, MS_SHARED|MS_REC, NULL);
}
static int
mount_bind (const char *src, const char *dest)
{
return mount (src, dest, NULL, MS_BIND, NULL);
}
char *
strconcat (const char *s1,
const char *s2,
const char *s3)
{
size_t len = 0;
char *res;
if (s1)
len += strlen (s1);
if (s2)
len += strlen (s2);
if (s3)
len += strlen (s3);
res = malloc (len + 1);
if (res == NULL)
return NULL;
*res = 0;
if (s1)
strcat (res, s1);
if (s2)
strcat (res, s2);
if (s3)
strcat (res, s3);
return res;
}
static void
update_env_var (const char *var, const char *dir, const char *default_dir)
{
const char *env;
char *value;
env = getenv (var);
if (env == NULL || *env == 0)
value = strconcat (dir, ":", default_dir);
else
value = strconcat (dir, ":", env);
if (value != NULL)
{
setenv (var, value, 1);
free (value);
}
}
int
main(int argc, char **argv)
{
char *session_dir;
const char *runtime_dir;
const char *homedir;
int res;
/* The initial code is run with a high permission euid
(at least CAP_SYS_ADMIN), so take lots of care. */
/* Switch effective uid to the user */
seteuid (getuid ());
if (argc == 1)
{
fprintf (stderr, "No executable specified\n");
return 1;
}
homedir = get_homedir ();
if (homedir == NULL)
goto error;
runtime_dir = getenv ("XDG_RUNTIME_DIR");
if (runtime_dir != NULL)
{
session_dir = build_path (runtime_dir, "sessiondir");
if (session_dir == NULL)
{
fprintf (stderr, "Out of memory\n");
goto error;
}
if (mkdir (session_dir, 0700) == -1 && errno != EEXIST)
{
fprintf (stderr, "Unable to create temporary session directory\n");
goto error;
}
}
else
{
/* Fall back to /tmp dir */
char tmpdir[] = "/tmp/.sessionXXXXXX";
session_dir = mkdtemp(tmpdir);
if (session_dir == NULL)
{
fprintf (stderr, "Unable to create temporary session directory\n");
goto error;
}
}
if (setuid (0) == -1)
{
perror ("Unable to regain root priviledges");
goto error;
}
/* Create a new mount namespace for the session */
res = unshare (CLONE_NEWNS);
if (res != 0)
{
perror ("Creating new namespace failed");
goto error;
}
/* Start with a clean slate, all mounts private */
res = mount_rprivate ("/");
if (res != 0)
{
perror ("Failed to make rprivate");
goto error;
}
/* Make /opt/session point to the session directory.
This will not propagate dure to the rprivate above. */
res = mount_bind (session_dir, SESSION_PREFIX);
if (res != 0)
{
perror ("Failed to bind session dir");
goto error;
}
/* Make the whole new namespace rshared so that later session
mounts are propagated to child bundle namespaces */
res = mount_rshared ("/");
if (res != 0)
{
perror ("Failed to make rshared");
goto error;
}
/* Except the session dir. */
res = mount (SESSION_PREFIX, SESSION_PREFIX,
NULL, MS_PRIVATE, NULL);
if (res != 0)
{
perror ("Failed to make session dir private");
goto error;
}
/* Now we have everything we need CAP_SYS_ADMIN for, so drop setuid */
setuid (getuid ());
make_symlink (session_dir, "bundles",
homedir, ".glick/bundles");
make_symlink (session_dir, "exports",
homedir, ".glick/exports");
update_env_var ("XDG_CONFIG_DIRS", SESSION_PREFIX "/exports/etc", "/etc/xdg");
update_env_var ("XDG_DATA_DIRS", SESSION_PREFIX "/exports/share", "/usr/share");
return execvp (argv[1], argv+1);
error:
/* Now we have everything we need CAP_SYS_ADMIN for, so drop setuid */
setuid (getuid ());
return execvp (argv[1], argv+1);
}