-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
pam_cgfs.c
2639 lines (2157 loc) · 64.8 KB
/
pam_cgfs.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pam-cgfs
*
* Copyright © 2016 Canonical, Inc
* Author: Serge Hallyn <serge.hallyn@ubuntu.com>
* Author: Christian Brauner <christian.brauner@ubuntu.com>
*
* When a user logs in, this pam module will create cgroups which the user may
* administer. It handles both pure cgroupfs v1 and pure cgroupfs v2, as well as
* mixed mounts, where some controllers are mounted in a standard cgroupfs v1
* hierarchy location (/sys/fs/cgroup/<controller>) and others are in the
* cgroupfs v2 hierarchy.
* Writeable cgroups are either created for all controllers or, if specified,
* for any controllers listed on the command line.
* The cgroup created will be "user/$user/0" for the first session,
* "user/$user/1" for the second, etc.
*
* Systems with a systemd init system are treated specially, both with respect
* to cgroupfs v1 and cgroupfs v2. For both, cgroupfs v1 and cgroupfs v2, We
* check whether systemd already placed us in a cgroup it created:
*
* user.slice/user-uid.slice/session-n.scope
*
* by checking whether uid == our uid. If it did, we simply chown the last
* part (session-n.scope). If it did not we create a cgroup as outlined above
* (user/$user/n) and chown it to our uid.
* The same holds for cgroupfs v2 where this assumptions becomes crucial:
* We __have to__ be placed in our under the cgroup systemd created for us on
* login, otherwise things like starting an xserver or similar will not work.
*
* All requested cgroups must be mounted under /sys/fs/cgroup/$controller,
* no messing around with finding mountpoints.
*
* See COPYING file for details.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/unistd.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <syslog.h>
#include <unistd.h>
#include "config.h"
#include "macro.h"
#include "utils.h"
#define PAM_SM_SESSION
#include <security/_pam_macros.h>
#include <security/pam_modules.h>
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#ifndef HAVE_STRLCAT
#include "include/strlcat.h"
#endif
#define pam_cgfs_debug_stream(stream, format, ...) \
do { \
fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__, \
__func__, __VA_ARGS__); \
} while (false)
#define pam_cgfs_error(format, ...) pam_cgfs_debug_stream(stderr, format, __VA_ARGS__)
#ifdef DEBUG
#define pam_cgfs_debug(format, ...) pam_cgfs_error(format, __VA_ARGS__)
#else
#define pam_cgfs_debug(format, ...)
#endif /* DEBUG */
/* Taken over modified from the kernel sources. */
#define NBITS 32 /* bits in uint32_t */
#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
static enum cg_mount_mode {
CGROUP_UNKNOWN = -1,
CGROUP_MIXED = 0,
CGROUP_PURE_V1 = 1,
CGROUP_PURE_V2 = 2,
CGROUP_UNINITIALIZED = 3,
} cg_mount_mode = CGROUP_UNINITIALIZED;
/* Common helper functions. Most of these have been taken from LXC. */
static void append_line(char **dest, size_t oldlen, char *new, size_t newlen);
static int append_null_to_list(void ***list);
static void batch_realloc(char **mem, size_t oldlen, size_t newlen);
static inline void clear_bit(unsigned bit, uint32_t *bitarr)
{
bitarr[bit / NBITS] &= ~(1 << (bit % NBITS));
}
static char *copy_to_eol(char *s);
static void free_string_list(char **list);
static char *get_mountpoint(char *line);
static bool get_uid_gid(const char *user, uid_t *uid, gid_t *gid);
static int handle_login(const char *user, uid_t uid, gid_t gid);
static inline bool is_set(unsigned bit, uint32_t *bitarr)
{
return (bitarr[bit / NBITS] & (1 << (bit % NBITS))) != 0;
}
static bool is_lxcfs(const char *line);
static bool is_cgv1(char *line);
static bool is_cgv2(char *line);
static void must_add_to_list(char ***clist, char *entry);
static void must_append_controller(char **klist, char **nlist, char ***clist,
char *entry);
static void must_append_string(char ***list, char *entry);
static void mysyslog(int err, const char *format, ...) __attribute__((sentinel));
static char *read_file(char *fnam);
static int recursive_rmdir(char *dirname);
static inline void set_bit(unsigned bit, uint32_t *bitarr)
{
bitarr[bit / NBITS] |= (1 << (bit % NBITS));
}
static bool string_in_list(char **list, const char *entry);
static char *string_join(const char *sep, const char **parts, bool use_as_prefix);
static void trim(char *s);
static bool write_int(char *path, int v);
/* cgroupfs prototypes. */
static bool cg_belongs_to_uid_gid(const char *path, uid_t uid, gid_t gid);
static uint32_t *cg_cpumask(char *buf, size_t nbits);
static bool cg_copy_parent_file(char *path, char *file);
static char *cg_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits);
static bool cg_enter(const char *cgroup);
static void cg_escape(void);
static bool cg_filter_and_set_cpus(char *path, bool am_initialized);
static ssize_t cg_get_max_cpus(char *cpulist);
static int cg_get_version_of_mntpt(const char *path);
static bool cg_init(uid_t uid, gid_t gid);
static void cg_mark_to_make_rw(char **list);
static void cg_prune_empty_cgroups(const char *user);
static bool cg_systemd_created_user_slice(const char *base_cgroup,
const char *init_cgroup,
const char *in, uid_t uid);
static bool cg_systemd_chown_existing_cgroup(const char *mountpoint,
const char *base_cgroup, uid_t uid,
gid_t gid,
bool systemd_user_slice);
static bool cg_systemd_under_user_slice_1(const char *in, uid_t uid);
static bool cg_systemd_under_user_slice_2(const char *base_cgroup,
const char *init_cgroup, uid_t uid);
static void cg_systemd_prune_init_scope(char *cg);
static bool is_lxcfs(const char *line);
/* cgroupfs v1 prototypes. */
struct cgv1_hierarchy {
char **controllers;
char *mountpoint;
char *base_cgroup;
char *fullcgpath;
char *init_cgroup;
bool create_rw_cgroup;
bool systemd_user_slice;
};
static struct cgv1_hierarchy **cgv1_hierarchies;
static void cgv1_add_controller(char **clist, char *mountpoint,
char *base_cgroup, char *init_cgroup);
static bool cgv1_controller_in_clist(char *cgline, char *c);
static bool cgv1_controller_lists_intersect(char **l1, char **l2);
static bool cgv1_controller_list_is_dup(struct cgv1_hierarchy **hlist,
char **clist);
static bool cgv1_create(const char *cgroup, uid_t uid, gid_t gid,
bool *existed);
static bool cgv1_create_one(struct cgv1_hierarchy *h, const char *cgroup,
uid_t uid, gid_t gid, bool *existed);
static bool cgv1_enter(const char *cgroup);
static void cgv1_escape(void);
static bool cgv1_get_controllers(char ***klist, char ***nlist);
static char *cgv1_get_current_cgroup(char *basecginfo, char *controller);
static char **cgv1_get_proc_mountinfo_controllers(char **klist, char **nlist,
char *line);
static bool cgv1_handle_cpuset_hierarchy(struct cgv1_hierarchy *h,
const char *cgroup);
static bool cgv1_handle_root_cpuset_hierarchy(struct cgv1_hierarchy *h);
static bool cgv1_init(uid_t uid, gid_t gid);
static void cgv1_mark_to_make_rw(char **clist);
static char *cgv1_must_prefix_named(char *entry);
static bool cgv1_prune_empty_cgroups(const char *user);
static bool cgv1_remove_one(struct cgv1_hierarchy *h, const char *cgroup);
static bool is_cgv1(char *line);
/* cgroupfs v2 prototypes. */
struct cgv2_hierarchy {
char **controllers;
char *mountpoint;
char *base_cgroup;
char *fullcgpath;
char *init_cgroup;
bool create_rw_cgroup;
bool systemd_user_slice;
};
/* Actually this should only be a single hierarchy. But for the sake of
* parallelism and because the layout of the cgroupfs v2 is still somewhat
* changing, we'll leave it as an array of structs.
*/
static struct cgv2_hierarchy **cgv2_hierarchies;
static void cgv2_add_controller(char **clist, char *mountpoint,
char *base_cgroup, char *init_cgroup,
bool systemd_user_slice);
static bool cgv2_create(const char *cgroup, uid_t uid, gid_t gid,
bool *existed);
static bool cgv2_enter(const char *cgroup);
static void cgv2_escape(void);
static char *cgv2_get_current_cgroup(int pid);
static bool cgv2_init(uid_t uid, gid_t gid);
static void cgv2_mark_to_make_rw(char **clist);
static bool cgv2_prune_empty_cgroups(const char *user);
static bool cgv2_remove(const char *cgroup);
static bool is_cgv2(char *line);
static int do_mkdir(const char *path, mode_t mode)
{
int saved_errno;
mode_t mask;
int r;
mask = umask(0);
r = mkdir(path, mode);
saved_errno = errno;
umask(mask);
errno = saved_errno;
return (r);
}
/* Create directory and (if necessary) its parents. */
static bool mkdir_parent(const char *root, char *path)
{
char *b, orig, *e;
if (strlen(path) < strlen(root))
return false;
if (strlen(path) == strlen(root))
return true;
b = path + strlen(root) + 1;
while (true) {
while (*b && (*b == '/'))
b++;
if (!*b)
return true;
e = b + 1;
while (*e && *e != '/')
e++;
orig = *e;
if (orig)
*e = '\0';
if (file_exists(path))
goto next;
if (do_mkdir(path, 0755) < 0) {
pam_cgfs_debug("Failed to create %s: %s\n", path, strerror(errno));
return false;
}
next:
if (!orig)
return true;
*e = orig;
b = e + 1;
}
return false;
}
/* Common helper functions. Most of these have been taken from LXC. */
static void mysyslog(int err, const char *format, ...)
{
va_list args;
va_start(args, format);
openlog("PAM-CGFS", LOG_CONS | LOG_PID, LOG_AUTH);
vsyslog(err, format, args);
va_end(args);
closelog();
}
/* realloc() pointer in batch sizes; do not fail. */
#define BATCH_SIZE 50
static void batch_realloc(char **mem, size_t oldlen, size_t newlen)
{
int newbatches = (newlen / BATCH_SIZE) + 1;
int oldbatches = (oldlen / BATCH_SIZE) + 1;
if (!*mem || newbatches > oldbatches)
*mem = must_realloc(*mem, newbatches * BATCH_SIZE);
}
/* Append lines as is to pointer; do not fail. */
static void append_line(char **dest, size_t oldlen, char *new, size_t newlen)
{
size_t full = oldlen + newlen;
batch_realloc(dest, oldlen, full + 1);
memcpy(*dest + oldlen, new, newlen + 1);
}
/* Read in whole file and return allocated pointer. */
static char *read_file(char *fnam)
{
FILE *f;
int linelen;
char *line = NULL, *buf = NULL;
size_t len = 0, fulllen = 0;
f = fopen(fnam, "r");
if (!f)
return NULL;
while ((linelen = getline(&line, &len, f)) != -1) {
append_line(&buf, fulllen, line, linelen);
fulllen += linelen;
}
fclose(f);
free(line);
return buf;
}
/* Given a pointer to a null-terminated array of pointers, realloc to add one
* entry, and point the new entry to NULL. Do not fail. Return the index to the
* second-to-last entry - that is, the one which is now available for use
* (keeping the list null-terminated).
*/
static int append_null_to_list(void ***list)
{
int newentry = 0;
if (*list)
for (; (*list)[newentry]; newentry++)
;
*list = must_realloc(*list, (newentry + 2) * sizeof(void **));
(*list)[newentry + 1] = NULL;
return newentry;
}
/* Append new entry to null-terminated array of pointer; make sure that array of
* pointers will still be null-terminated.
*/
static void must_append_string(char ***list, char *entry)
{
int newentry;
char *copy;
newentry = append_null_to_list((void ***)list);
copy = must_copy_string(entry);
(*list)[newentry] = copy;
}
/* Remove newlines from string. */
static void trim(char *s)
{
size_t len = strlen(s);
while ((len > 0) && s[len - 1] == '\n')
s[--len] = '\0';
}
/* Make allocated copy of string. End of string is taken to be '\n'. */
static char *copy_to_eol(char *s)
{
char *newline, *sret;
size_t len;
newline = strchr(s, '\n');
if (!newline)
return NULL;
len = newline - s;
sret = must_realloc(NULL, len + 1);
memcpy(sret, s, len);
sret[len] = '\0';
return sret;
}
/* Check if given entry under /proc/<pid>/mountinfo is a fuse.lxcfs mount. */
static bool is_lxcfs(const char *line)
{
char *p = strstr(line, " - ");
if (!p)
return false;
return strncmp(p, " - fuse.lxcfs ", 14) == 0;
}
/* Check if given entry under /proc/<pid>/mountinfo is a cgroupfs v1 mount. */
static bool is_cgv1(char *line)
{
char *p = strstr(line, " - ");
if (!p)
return false;
return strncmp(p, " - cgroup ", 10) == 0;
}
/* Check if given entry under /proc/<pid>/mountinfo is a cgroupfs v2 mount. */
static bool is_cgv2(char *line)
{
char *p = strstr(line, " - ");
if (!p)
return false;
return strncmp(p, " - cgroup2 ", 11) == 0;
}
/* Given a null-terminated array of strings, check whether @entry is one of the
* strings
*/
static bool string_in_list(char **list, const char *entry)
{
char **it;
for (it = list; it && *it; it++)
if (strcmp(*it, entry) == 0)
return true;
return false;
}
/*
* Creates a null-terminated array of strings, made by splitting the entries in
* @str on each @sep. Caller is responsible for calling free_string_list.
*/
static char **make_string_list(const char *str, const char *sep)
{
char *copy, *tok;
char *saveptr = NULL;
char **clist = NULL;
copy = must_copy_string(str);
for (tok = strtok_r(copy, sep, &saveptr); tok;
tok = strtok_r(NULL, sep, &saveptr))
must_add_to_list(&clist, tok);
free(copy);
return clist;
}
/* Gets the length of a null-terminated array of strings. */
static size_t string_list_length(char **list)
{
size_t len = 0;
char **it;
for (it = list; it && *it; it++)
len++;
return len;
}
/* Free null-terminated array of strings. */
static void free_string_list(char **list)
{
char **it;
for (it = list; it && *it; it++)
free(*it);
free(list);
}
/* Write single integer to file. */
static bool write_int(char *path, int v)
{
FILE *f;
bool ret = true;
f = fopen(path, "w");
if (!f)
return false;
if (fprintf(f, "%d\n", v) < 0)
ret = false;
if (fclose(f) != 0)
ret = false;
return ret;
}
/* Recursively remove directory and its parents. */
static int recursive_rmdir(char *dirname)
{
struct dirent *direntp;
DIR *dir;
int r = 0;
dir = opendir(dirname);
if (!dir)
return -ENOENT;
while ((direntp = readdir(dir))) {
struct stat st;
char *pathname;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
pathname = must_make_path(dirname, direntp->d_name, NULL);
if (lstat(pathname, &st)) {
if (!r)
pam_cgfs_debug("Failed to stat %s\n", pathname);
r = -1;
goto next;
}
if (!S_ISDIR(st.st_mode))
goto next;
if (recursive_rmdir(pathname) < 0)
r = -1;
next:
free(pathname);
}
if (rmdir(dirname) < 0) {
if (!r)
pam_cgfs_debug("Failed to delete %s: %s\n", dirname, strerror(errno));
r = -1;
}
if (closedir(dir) < 0) {
if (!r)
pam_cgfs_debug("Failed to delete %s: %s\n", dirname, strerror(errno));
r = -1;
}
return r;
}
/* Add new entry to null-terminated array of pointers. Make sure array is still
* null-terminated.
*/
static void must_add_to_list(char ***clist, char *entry)
{
int newentry;
newentry = append_null_to_list((void ***)clist);
(*clist)[newentry] = must_copy_string(entry);
}
/* Get mountpoint from a /proc/<pid>/mountinfo line. */
static char *get_mountpoint(char *line)
{
int i;
char *p, *sret, *p2;
size_t len;
p = line;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
p2 = strchr(p, ' ');
if (p2)
*p2 = '\0';
len = strlen(p);
sret = must_realloc(NULL, len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
}
/* Create list of cgroupfs v1 controller found under /proc/self/cgroup. Skips
* the 0::/some/path cgroupfs v2 hierarchy listed. Splits controllers into
* kernel controllers (@klist) and named controllers (@nlist).
*/
static bool cgv1_get_controllers(char ***klist, char ***nlist)
{
FILE *f;
char *line = NULL;
size_t len = 0;
f = fopen("/proc/self/cgroup", "r");
if (!f)
return false;
while (getline(&line, &len, f) != -1) {
char *p, *p2, *tok;
char *saveptr = NULL;
p = strchr(line, ':');
if (!p)
continue;
p++;
p2 = strchr(p, ':');
if (!p2)
continue;
*p2 = '\0';
/* Skip the v2 hierarchy. */
if ((p2 - p) == 0)
continue;
for (tok = strtok_r(p, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
if (strncmp(tok, "name=", 5) == 0)
must_append_string(nlist, tok);
else
must_append_string(klist, tok);
}
}
free(line);
fclose(f);
return true;
}
/* Get list of controllers for cgroupfs v2 hierarchy by looking at
* cgroup.controllers and/or cgroup.subtree_control of a given (parent) cgroup.
static bool cgv2_get_controllers(char ***klist)
{
return -ENOSYS;
}
*/
/* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */
static char *cgv2_get_current_cgroup(int pid)
{
int ret;
char *cgroups_v2;
char *current_cgroup;
char *copy = NULL;
/* The largest integer that can fit into long int is 2^64. This is a
* 20-digit number. */
#define __PIDLEN /* /proc */ 5 + /* /pid-to-str */ 21 + /* /cgroup */ 7 + /* \0 */ 1
char path[__PIDLEN];
ret = snprintf(path, __PIDLEN, "/proc/%d/cgroup", pid);
if (ret < 0 || ret >= __PIDLEN)
return NULL;
cgroups_v2 = read_file(path);
if (!cgroups_v2)
return NULL;
current_cgroup = strstr(cgroups_v2, "0::/");
if (!current_cgroup)
goto cleanup_on_err;
current_cgroup = current_cgroup + 3;
copy = copy_to_eol(current_cgroup);
if (!copy)
goto cleanup_on_err;
cleanup_on_err:
free(cgroups_v2);
if (copy)
trim(copy);
return copy;
}
/* Given two null-terminated lists of strings, return true if any string is in
* both.
*/
static bool cgv1_controller_lists_intersect(char **l1, char **l2)
{
char **it;
if (!l2)
return false;
for (it = l1; it && *it; it++)
if (string_in_list(l2, *it))
return true;
return false;
}
/* For a null-terminated list of controllers @clist, return true if any of those
* controllers is already listed the null-terminated list of hierarchies @hlist.
* Realistically, if one is present, all must be present.
*/
static bool cgv1_controller_list_is_dup(struct cgv1_hierarchy **hlist, char **clist)
{
struct cgv1_hierarchy **it;
for (it = hlist; it && *it; it++)
if ((*it)->controllers)
if (cgv1_controller_lists_intersect((*it)->controllers, clist))
return true;
return false;
}
/* Set boolean to mark controllers under which we are supposed create a
* writeable cgroup.
*/
static void cgv1_mark_to_make_rw(char **clist)
{
struct cgv1_hierarchy **it;
for (it = cgv1_hierarchies; it && *it; it++)
if ((*it)->controllers)
if (cgv1_controller_lists_intersect((*it)->controllers, clist) ||
string_in_list(clist, "all"))
(*it)->create_rw_cgroup = true;
}
/* Set boolean to mark whether we are supposed to create a writeable cgroup in
* the cgroupfs v2 hierarchy.
*/
static void cgv2_mark_to_make_rw(char **clist)
{
if (string_in_list(clist, "unified") || string_in_list(clist, "all"))
if (cgv2_hierarchies)
(*cgv2_hierarchies)->create_rw_cgroup = true;
}
/* Wrapper around cgv{1,2}_mark_to_make_rw(). */
static void cg_mark_to_make_rw(char **clist)
{
cgv1_mark_to_make_rw(clist);
cgv2_mark_to_make_rw(clist);
}
/* Prefix any named controllers with "name=", e.g. "name=systemd". */
static char *cgv1_must_prefix_named(char *entry)
{
char *s;
int ret;
size_t len;
len = strlen(entry);
s = must_realloc(NULL, len + 6);
ret = snprintf(s, len + 6, "name=%s", entry);
if (ret < 0 || (size_t)ret >= (len + 6)) {
free(s);
return NULL;
}
return s;
}
/* Append kernel controller in @klist or named controller in @nlist to @clist */
static void must_append_controller(char **klist, char **nlist, char ***clist, char *entry)
{
int newentry;
char *copy;
if (string_in_list(klist, entry) && string_in_list(nlist, entry))
return;
newentry = append_null_to_list((void ***)clist);
if (strncmp(entry, "name=", 5) == 0)
copy = must_copy_string(entry);
else if (string_in_list(klist, entry))
copy = must_copy_string(entry);
else
copy = cgv1_must_prefix_named(entry);
(*clist)[newentry] = copy;
}
/* Get the controllers from a mountinfo line. There are other ways we could get
* this info. For lxcfs, field 3 is /cgroup/controller-list. For cgroupfs, we
* could parse the mount options. But we simply assume that the mountpoint must
* be /sys/fs/cgroup/controller-list
*/
static char **cgv1_get_proc_mountinfo_controllers(char **klist, char **nlist, char *line)
{
int i;
char *p, *p2, *tok;
char *saveptr = NULL;
char **aret = NULL;
p = line;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
if (strncmp(p, "/sys/fs/cgroup/", 15) != 0)
return NULL;
p += 15;
p2 = strchr(p, ' ');
if (!p2)
return NULL;
*p2 = '\0';
for (tok = strtok_r(p, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr))
must_append_controller(klist, nlist, &aret, tok);
return aret;
}
/* Check if a cgroupfs v2 controller is present in the string @cgline. */
static bool cgv1_controller_in_clist(char *cgline, char *c)
{
size_t len;
char *tok, *eol, *tmp;
char *saveptr = NULL;
eol = strchr(cgline, ':');
if (!eol)
return false;
len = eol - cgline;
tmp = alloca(len + 1);
memcpy(tmp, cgline, len);
tmp[len] = '\0';
for (tok = strtok_r(tmp, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
if (strcmp(tok, c) == 0)
return true;
}
return false;
}
/* Get current cgroup from the /proc/<pid>/cgroup file passed in via @basecginfo
* of a given cgv1 controller passed in via @controller.
*/
static char *cgv1_get_current_cgroup(char *basecginfo, char *controller)
{
char *p;
p = basecginfo;
while (true) {
p = strchr(p, ':');
if (!p)
return NULL;
p++;
if (cgv1_controller_in_clist(p, controller)) {
p = strchr(p, ':');
if (!p)
return NULL;
p++;
return copy_to_eol(p);
}
p = strchr(p, '\n');
if (!p)
return NULL;
p++;
}
return NULL;
}
/* Remove /init.scope from string @cg. This will mostly affect systemd-based
* systems.
*/
#define INIT_SCOPE "/init.scope"
static void cg_systemd_prune_init_scope(char *cg)
{
char *point;
if (!cg)
return;
point = cg + strlen(cg) - strlen(INIT_SCOPE);
if (point < cg)
return;
if (strcmp(point, INIT_SCOPE) == 0) {
if (point == cg)
*(point + 1) = '\0';
else
*point = '\0';
}
}
/* Add new info about a mounted cgroupfs v1 hierarchy. Includes the controllers
* mounted into that hierarchy (e.g. cpu,cpuacct), the mountpoint of that
* hierarchy (/sys/fs/cgroup/<controller>, the base cgroup of the current
* process gathered from /proc/self/cgroup, and the init cgroup of PID1 gathered
* from /proc/1/cgroup.
*/
static void cgv1_add_controller(char **clist, char *mountpoint, char *base_cgroup, char *init_cgroup)
{
struct cgv1_hierarchy *new;
int newentry;
new = must_realloc(NULL, sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
new->base_cgroup = base_cgroup;
new->fullcgpath = NULL;
new->create_rw_cgroup = false;
new->init_cgroup = init_cgroup;
new->systemd_user_slice = false;
newentry = append_null_to_list((void ***)&cgv1_hierarchies);
cgv1_hierarchies[newentry] = new;
}
/* Add new info about the mounted cgroupfs v2 hierarchy. Can (but doesn't
* currently) include the controllers mounted into the hierarchy (e.g. memory,
* pids, blkio), the mountpoint of that hierarchy (Should usually be
* /sys/fs/cgroup but some init systems seems to think it might be a good idea
* to also mount empty cgroupfs v2 hierarchies at /sys/fs/cgroup/systemd.), the
* base cgroup of the current process gathered from /proc/self/cgroup, and the
* init cgroup of PID1 gathered from /proc/1/cgroup.
*/
static void cgv2_add_controller(char **clist, char *mountpoint, char *base_cgroup, char *init_cgroup, bool systemd_user_slice)
{
struct cgv2_hierarchy *new;
int newentry;
new = must_realloc(NULL, sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
new->base_cgroup = base_cgroup;
new->fullcgpath = NULL;
new->create_rw_cgroup = false;
new->init_cgroup = init_cgroup;
new->systemd_user_slice = systemd_user_slice;
newentry = append_null_to_list((void ***)&cgv2_hierarchies);
cgv2_hierarchies[newentry] = new;
}
/* In Ubuntu 14.04, the paths created for us were
* '/user/$uid.user/$something.session' This can be merged better with
* systemd_created_slice_for_us(), but keeping it separate makes it easier to
* reason about the correctness.
*/
static bool cg_systemd_under_user_slice_1(const char *in, uid_t uid)
{
char *p;
size_t len;
int id;
char *copy = NULL;
bool bret = false;
copy = must_copy_string(in);
if (strlen(copy) < strlen("/user/1.user/1.session"))
goto cleanup;
p = copy + strlen(copy) - 1;
/* skip any trailing '/' (shouldn't be any, but be sure) */
while (p >= copy && *p == '/')
*(p--) = '\0';
if (p < copy)
goto cleanup;
/* Get last path element */
while (p >= copy && *p != '/')
p--;