forked from jjk-jacky/pam_rundir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pam_rundir.c
450 lines (376 loc) · 10.5 KB
/
pam_rundir.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* pam_rundir - Copyright (C) 2015 Olivier Brunel
*
* pam_rundir.c
* Copyright (C) 2015 Olivier Brunel <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see http://www.gnu.org/licenses/
*/
#include "config.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/prctl.h>
#include <linux/securebits.h>
#include <string.h>
#include <pwd.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#define PAM_SM_SESSION
#include <security/pam_modules.h>
#include <security/pam_appl.h>
#define FLAG_NAME "pam_rundir_has_counted"
static int
ensure_parent_dir (void)
{
int r = 1;
mode_t um = umask (S_IWOTH);
if (mkdir (PARENT_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0
&& errno != EEXIST)
r = 0;
umask (um);
return r;
}
static int
intlen (int n)
{
int l;
for (l = 1; ; ++l)
{
if (n < 10)
break;
n /= 10;
}
return l;
}
static void
print_int (char *s, int n, int l)
{
s += l;
for (;;)
{
const char digits[] = "0123456789";
*--s = digits[n % 10];
if (n < 10)
break;
n /= 10;
}
}
static int
open_and_lock (const char *file)
{
int fd;
do { fd = open (file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); }
while (fd < 0 && errno == EINTR);
if (fd < 0)
return fd;
if (flock (fd, LOCK_EX) < 0)
{
close (fd);
return -1;
}
return fd;
}
static inline void
print_filename (char *s, int uid, int l)
{
/* construct file name, e.g: "/run/users/.1000" */
memcpy (s, PARENT_DIR, sizeof (PARENT_DIR) - 1);
s[sizeof (PARENT_DIR) - 1] = '/';
s[sizeof (PARENT_DIR)] = '.';
print_int (s + sizeof (PARENT_DIR) + 1, uid, l);
s[sizeof (PARENT_DIR) + 1 + l] = '\0';
}
static int
read_counter (int fd)
{
int count = 0;
/* read counter in file, as ascii string */
for (;;)
{
char buf[4];
int p;
int r;
r = read (fd, buf, sizeof (buf));
if (r == 0)
break;
else if (r < 0)
{
if (errno == EINTR)
continue;
else
return -1;
}
else if (count == 0 && r == 1 && buf[0] == '-')
/* special case: dir not usable, but not a failure */
return -2;
for (p = 0; r > 0; --r, ++p)
{
if (buf[p] < '0' || buf[p] > '9')
return -1;
count *= 10;
count += buf[p] - '0';
}
}
return count;
}
/* basically, this is called when we tried to update the counter but failed,
* leaving the file in an invalid state (i.e. only partial write, or no
* truncate).
* So here, we try to make the file "properly invalid" so any further attempt to
* read it will lead to a no-op (because of invalid data). Obviously though, if
* we fail to e.g. seek or write, we can't do anything else...
* (Anyhow, this will likely never be called.)
*/
static void
emergency_invalidate_counter (int fd)
{
int r;
if (lseek (fd, 0, SEEK_SET) < 0)
return;
do { r = write (fd, "-", 1); }
while (r < 0 && errno == EINTR);
if (r == 1)
do { r = ftruncate (fd, 1); }
while (r < 0 && errno == EINTR);
}
static int
write_counter (int fd, int count)
{
int r;
int l;
r = lseek (fd, 0, SEEK_SET);
if (r < 0)
return r;
l = (count >= 0) ? intlen (count) : 1;
{
char buf[l];
if (count >= 0)
print_int (buf, count, l);
else
buf[0] = '-';
for (;;)
{
int w = 0;
r = write (fd, buf + w, l - w);
if (r < 0)
{
if (errno == EINTR)
continue;
if (w > 0)
emergency_invalidate_counter (fd);
return -1;
}
w += r;
if (w == l)
break;
}
do { r = ftruncate (fd, l); }
while (r < 0 && errno == EINTR);
if (r < 0)
emergency_invalidate_counter (fd);
}
return r;
}
static int
rmrf (const char *path)
{
int r = 0;
DIR *dir;
struct dirent *dp;
int lp;
if (unlink (path) == 0)
return 0;
else if (errno != EISDIR)
return -1;
dir = opendir (path);
if (!dir)
return -1;
lp = strlen (path);
for (dp = readdir (dir); dp != NULL; dp = readdir (dir))
{
if (strcmp (dp->d_name, ".") != 0 && strcmp (dp->d_name, "..") != 0)
{
int l = lp + strlen (dp->d_name) + 2;
char name[l];
memcpy (name, path, lp);
name[lp] = '/';
memcpy (name + lp + 1, dp->d_name, l - lp - 1);
r += rmrf (name);
}
}
closedir (dir);
if (rmdir (path) < 0)
--r;
return r;
}
PAM_EXTERN int
pam_sm_close_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int r;
const char *user;
struct passwd *pw;
int l;
/* did we add to the counter on open_session (i.e. anything to do) ? */
r = pam_get_data (pamh, FLAG_NAME, (const void **) &pw);
if (r != PAM_SUCCESS)
return (r == PAM_NO_MODULE_DATA) ? PAM_SUCCESS : r;
if (geteuid() != 0)
return PAM_SESSION_ERR;
if (!ensure_parent_dir ())
return PAM_SESSION_ERR;
r = pam_get_user (pamh, &user, NULL);
if (r != PAM_SUCCESS)
return PAM_USER_UNKNOWN;
pw = getpwnam (user);
if (!pw)
return PAM_USER_UNKNOWN;
/* get length for uid as ascii string, i.e. in file/folder name */
l = intlen ((int) pw->pw_uid);
{
char file[sizeof (PARENT_DIR) + l + 2];
int fd;
int count = 0;
print_filename (file, (int) pw->pw_uid, l);
fd = open_and_lock (file);
if (fd < 0)
return PAM_SESSION_ERR;
count = read_counter (fd);
if (count < 0)
{
/* -2: dir not usable, but not a failure */
r = (count == -2) ? 0 : -1;
goto done;
}
/* make sure we don't go below zero, just in case */
if (count > 0)
--count;
if (count == 0)
{
/* construct runtime dir name, i.e. remove the dot before uid */
memmove (file + sizeof (PARENT_DIR), file + sizeof (PARENT_DIR) + 1, l + 1);
r = rmrf (file);
if (r < 0)
count = -1;
}
r = write_counter (fd, count);
if (r < 0)
goto done;
if (count == -1)
r = -1;
done:
close (fd); /* also unlocks */
}
return (r == 0) ? PAM_SUCCESS : PAM_SESSION_ERR;
}
PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int r;
const char *user;
struct passwd *pw;
int l;
if (geteuid() != 0)
return PAM_SESSION_ERR;
if (!ensure_parent_dir ())
return PAM_SESSION_ERR;
r = pam_get_user (pamh, &user, NULL);
if (r != PAM_SUCCESS)
return PAM_USER_UNKNOWN;
pw = getpwnam (user);
if (!pw)
return PAM_USER_UNKNOWN;
/* get length for uid as ascii string, i.e. in file/folder name */
l = intlen ((int) pw->pw_uid);
{
char file[sizeof (PARENT_DIR) + l + 2];
int fd;
int count = 0;
int secbits = -1;
print_filename (file, (int) pw->pw_uid, l);
fd = open_and_lock (file);
if (fd < 0)
return PAM_SESSION_ERR;
count = read_counter (fd);
if (count < 0)
{
/* -2: dir not usable, but not a failure */
r = (count == -2) ? 0 : -1;
goto done;
}
/* construct runtime dir name, i.e. remove the dot before uid */
memmove (file + sizeof (PARENT_DIR), file + sizeof (PARENT_DIR) + 1, l + 1);
/* update counter now, so we don't have to undo folder creation if
* updating the counter fails, etc. Having a count with a failure to
* create the folder isn't that big a deal (plus rare enough) to be
* worthy of complicating things. */
r = write_counter (fd, ++count);
if (r < 0)
goto done;
/* flag for processing on close_session */
if (pam_set_data (pamh, FLAG_NAME, (void *) 1, NULL) != PAM_SUCCESS)
{
/* well shit... try to revert, though we can't do nothing if it
* fails. A PAM_BUF_ERR (only possible error) should be pretty rare
* though (especially combined with a failure to re-write). */
write_counter (fd, --count);
r = -1;
goto done;
}
/* to bypass permission checks for mkdir, in case it isn't group
* writable */
secbits = prctl (PR_GET_SECUREBITS);
if (secbits != -1)
prctl (PR_SET_SECUREBITS, (unsigned long) secbits | SECBIT_NO_SETUID_FIXUP);
/* set euid so if we do create the dir, it is own by the user */
if (seteuid (pw->pw_uid) < 0)
{
r = -1;
goto done;
}
if (mkdir (file, S_IRWXU) == 0 || errno == EEXIST)
{
l = strlen (file);
char buf[sizeof (VAR_NAME) + 1 + l];
memcpy (buf, VAR_NAME, sizeof (VAR_NAME) - 1);
buf[sizeof (VAR_NAME) - 1] = '=';
memcpy (buf + sizeof (VAR_NAME), file, l + 1);
pam_putenv (pamh, buf);
}
/* restore */
if (seteuid (0) < 0)
{
r = -1;
goto done;
}
done:
if (secbits != -1)
prctl (PR_SET_SECUREBITS, (unsigned long) secbits);
close (fd); /* also unlocks */
}
return (r == 0) ? PAM_SUCCESS : PAM_SESSION_ERR;
}
#ifdef PAM_STATIC
struct pam_module _pam_rundir_modstruct = {
"pam_rundir",
NULL,
NULL,
NULL,
pam_sm_open_session,
pam_sm_close_session,
NULL
};
#endif