forked from lxc/cgmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgmanager.c
1586 lines (1347 loc) · 38.9 KB
/
cgmanager.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
/* cgmanager
*
* Copyright © 2013 Stephane Graber
* Author: Stephane Graber <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <frontend.h>
#include <sys/resource.h>
#include <sys/vfs.h>
#include <linux/fs.h>
/*
* Maximum depth of directories we allow in Create
* Default is 16. Figure 4 directories per level of container
* nesting (/user/1000.user/c2.session/c1), that lets us nest
* 4 containers deep.
*/
static int maxdepth = 16;
/* GetPidCgroup */
int get_pid_cgroup_main(void *parent, char *controller, struct ucred p,
struct ucred r, struct ucred v, char **output)
{
char rcgpath[MAXPATHLEN], vcgpath[MAXPATHLEN];
if (!(prune_verify_comounts(controller))) {
nih_error("%s: Multiple controllers given: %s",
__func__, controller);
return -1;
}
// Get r's current cgroup in rcgpath
if (!compute_pid_cgroup(r.pid, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
// Get v's cgroup in vcgpath
if (!compute_pid_cgroup(v.pid, controller, "", vcgpath, NULL)) {
nih_error("%s: Could not determine the victim cgroup for %s",
__func__, controller);
return -1;
}
// Make sure v's cgroup is under r's
int rlen = strlen(rcgpath);
if (strncmp(rcgpath, vcgpath, rlen) != 0) {
nih_error("%s: v (%d)'s cgroup is not below r (%d)'s", __func__,
v.pid, r.pid);
return -1;
}
if (strlen(vcgpath) == rlen)
*output = NIH_MUST (nih_strdup(parent, "/") );
else
*output = NIH_MUST (nih_strdup(parent, vcgpath + rlen + 1) );
return 0;
}
/* GetPidCgroupAbs */
int get_pid_cgroup_abs_main(void *parent, char *controller,struct ucred p,
struct ucred r, struct ucred v, char **output)
{
char rcgpath[MAXPATHLEN], vcgpath[MAXPATHLEN];
if (!(prune_verify_comounts(controller))) {
nih_error("%s: Multiple controllers given: %s",
__func__, controller);
return -1;
}
// Get p's current cgroup in rcgpath
if (!compute_proxy_cgroup(p.pid, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
// Get v's cgroup in vcgpath
if (!compute_pid_cgroup(v.pid, controller, "", vcgpath, NULL)) {
nih_error("%s: Could not determine the victim cgroup for %s",
__func__, controller);
return -1;
}
// Make sure v's cgroup is under p's
int rlen = strlen(rcgpath);
if (strncmp(rcgpath, vcgpath, rlen) != 0) {
nih_error("%s: v (%d)'s cgroup is not below p (%d)'s", __func__,
v.pid, p.pid);
return -1;
}
if (strlen(vcgpath) == rlen)
*output = NIH_MUST (nih_strdup(parent, "/") );
else
*output = NIH_MUST (nih_strdup(parent, vcgpath + rlen) );
return 0;
}
static bool victim_under_proxy_cgroup(char *rcgpath, pid_t v,
const char *controller)
{
char vcgpath[MAXPATHLEN];
if (!compute_pid_cgroup(v, controller, "", vcgpath, NULL)) {
nih_error("%s: Could not determine the victim's cgroup for %s",
__func__, controller);
return false;
}
if (strncmp(vcgpath, rcgpath, strlen(rcgpath)) != 0)
return false;
return true;
}
int per_ctrl_move_pid_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v, bool escape)
{
char rcgpath[MAXPATHLEN], path[MAXPATHLEN];
bool unified = false;
FILE *f;
pid_t query = r.pid;
size_t maxlen;
if (is_unified_controller(controller))
unified = true;
// Get r's current cgroup in rcgpath
if (escape)
query = p.pid;
if (!compute_proxy_cgroup(query, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
// If the victim is not under proxy's cgroup, refuse
if (!victim_under_proxy_cgroup(rcgpath, v.pid, controller)) {
nih_error("%s: victim's cgroup is not under proxy's (p.uid %u)", __func__, p.uid);
return -1;
}
if (unified) {
/* rcgpath + / + cgroup + "/.cgm_leaf/cgroup.procs" + \0 */
maxlen = strlen(rcgpath) + strlen(cgroup) + strlen(U_LEAF "/cgroup.procs") + 2;
} else {
/* rcgpath + / + cgroup + /tasks + \0 */
maxlen = strlen(rcgpath) + strlen(cgroup) + 8;
}
if (maxlen > MAXPATHLEN) {
nih_error("%s: Path name too long", __func__);
return -1;
}
strcpy(path, rcgpath);
strncat(path, "/", MAXPATHLEN-1);
strncat(path, cgroup, MAXPATHLEN-1);
if (realpath_escapes(path, rcgpath)) {
nih_error("%s: Invalid path %s", __func__, path);
return -1;
}
// is r allowed to descend under the parent dir?
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_error("%s: pid %d (uid %u gid %u) may not read under %s", __func__,
r.pid, r.uid, r.gid, path);
return -1;
}
// is r allowed to write to tasks file?
if (unified) {
if (!ensure_leafdir(controller, path))
return -1;
strcat(path, U_LEAF);
strcat(path, "/cgroup.procs");
} else {
strncat(path, "/tasks", MAXPATHLEN-1);
}
if (!may_access(r.pid, r.uid, r.gid, path, O_WRONLY)) {
nih_error("%s: pid %d (uid %u gid %u) may not write to %s", __func__,
r.pid, r.uid, r.gid, path);
return -1;
}
f = fopen(path, "w");
if (!f) {
nih_error("%s: Failed to open %s", __func__, path);
return -1;
}
if (fprintf(f, "%d\n", v.pid) < 0) {
fclose(f);
nih_error("%s: Failed to write %d to %s", __func__, v.pid, path);
return -1;
}
if (fclose(f) != 0) {
nih_error("%s: Failed to write %d to %s", __func__, v.pid, path);
return -1;
}
nih_info(_("%d moved to %s:%s by %d's request"), v.pid,
controller, cgroup, r.pid);
return 0;
}
int do_move_pid_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v, bool escape)
{
nih_local char *c = NULL;
char *tok;
int ret;
int while_ret = 0;
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
// verify that ucred.pid may move target pid
if (!may_move_pid(r.pid, r.uid, v.pid)) {
nih_error("%s: %d may not move %d", __func__, r.pid, v.pid);
return -1;
}
if (strcmp(controller, "all") != 0 && !strchr(controller, ','))
return per_ctrl_move_pid_main(controller, cgroup, p, r, v, escape);
if (strcmp(controller, "all") == 0) {
if (!all_controllers)
return 0;
c = NIH_MUST( nih_strdup(NULL, all_controllers) );
} else {
c = NIH_MUST( nih_strdup(NULL, controller) );
do_prune_comounts(c);
}
tok = strtok(c, ",");
while (tok) {
ret = per_ctrl_move_pid_main(tok, cgroup, p, r, v, escape);
/* Save error for later (but ignore permission denied, -2),
but try to complete rest of moves anyway */
if (ret != 0 && ret != -2)
while_ret = -1;
tok = strtok(NULL, ",");
}
return while_ret;
}
int move_pid_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v)
{
if (cgroup[0] == '/') {
// We could try to be accomodating, but let's not fool around right now
nih_error("%s: Bad requested cgroup path: %s", __func__, cgroup);
return -1;
}
return do_move_pid_main(controller, cgroup, p, r, v, false);
}
int move_pid_abs_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v)
{
return do_move_pid_main(controller, cgroup, p, r, v, true);
}
int do_create_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, int32_t *existed)
{
int ret, depth;
char rcgpath[MAXPATHLEN], path[MAXPATHLEN], dirpath[MAXPATHLEN];
nih_local char *copy = NULL;
size_t cgroup_len;
char *p1, *p2, oldp2;
*existed = 1;
// Get r's current cgroup in rcgpath
if (!compute_pid_cgroup(r.pid, controller, "", rcgpath, &depth)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
if (depth > maxdepth) {
nih_error("%s: Cgroup too deep: %s/%s", __func__, rcgpath, cgroup);
return -1;
}
cgroup_len = strlen(cgroup);
if (strlen(rcgpath) + cgroup_len > MAXPATHLEN) {
nih_error("%s: Path name too long", __func__);
return -1;
}
copy = NIH_MUST( nih_strndup(NULL, cgroup, cgroup_len) );
strcpy(path, rcgpath);
strcpy(dirpath, rcgpath);
for (p1=copy; *p1; p1 = p2) {
*existed = -1;
for (p2=p1; *p2 && *p2 != '/'; p2++);
oldp2 = *p2;
*p2 = '\0';
if (strcmp(p1, "..") == 0) {
nih_error("%s: Invalid cgroup path at create: %s", __func__, p1);
return -1;
}
strncat(path, "/", MAXPATHLEN-1);
strncat(path, p1, MAXPATHLEN-1);
if (dir_exists(path)) {
*existed = 1;
// TODO - properly use execute perms
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_error("%s: pid %d (uid %u gid %u) may not look under %s", __func__,
r.pid, r.uid, r.gid, path);
return -2;
}
goto next;
}
if (!may_access(r.pid, r.uid, r.gid, dirpath, O_RDWR)) {
nih_error("%s: pid %d (uid %u gid %u) may not create under %s", __func__,
r.pid, r.uid, r.gid, dirpath);
return -2;
}
ret = mkdir(path, 0755);
if (ret < 0) { // Should we ignore EEXIST? Ok, but don't chown.
if (errno == EEXIST) {
*existed = 1;
goto next;
}
nih_error("%s: failed to create %s", __func__, path);
return -2;
}
if (!unified_copy_controllers(controller, path)) {
nih_error("%s: Failed to set cg controllers on %s", __func__,
path);
rmdir(path);
return -1;
}
if (!chown_cgroup_path(path, r.uid, r.gid, true)) {
nih_error("%s: Failed to change ownership on %s to %u:%u", __func__,
path, r.uid, r.gid);
rmdir(path);
return -1;
}
*existed = -1;
next:
strncat(dirpath, "/", MAXPATHLEN-1);
strncat(dirpath, p1, MAXPATHLEN-1);
*p2 = oldp2;
if (*p2)
p2++;
}
nih_info(_("Created %s for %d (%u:%u)"), path, r.pid,
r.uid, r.gid);
return 0;
}
int create_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, int32_t *existed)
{
nih_local char *c = NULL;
char *tok;
int ret;
*existed = -1;
if (!cgroup || ! *cgroup) // nothing to do
return 0;
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (strcmp(controller, "all") != 0 && !strchr(controller, ','))
return do_create_main(controller, cgroup, p, r, existed);
if (strcmp(controller, "all") == 0) {
if (!all_controllers)
return 0;
c = NIH_MUST( nih_strdup(NULL, all_controllers) );
} else {
c = NIH_MUST( nih_strdup(NULL, controller) );
do_prune_comounts(c);
}
tok = strtok(c, ",");
while (tok) {
int32_t e = 1;
ret = do_create_main(tok, cgroup, p, r, &e);
if (ret == -2) // permission denied - ignore for group requests
goto next;
if (ret != 0)
return -1;
if (e == 1)
*existed = 1;
next:
tok = strtok(NULL, ",");
}
return 0;
}
int do_chown_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v)
{
char rcgpath[MAXPATHLEN];
nih_local char *path = NULL;
// Get r's current cgroup in rcgpath
if (!compute_pid_cgroup(r.pid, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
/* rcgpath + / + cgroup + \0 */
if (strlen(rcgpath) + strlen(cgroup) > MAXPATHLEN+2) {
nih_error("%s: Path name too long", __func__);
return -1;
}
path = NIH_MUST( nih_sprintf(NULL, "%s/%s", rcgpath, cgroup) );
if (realpath_escapes(path, rcgpath)) {
nih_error("%s: Invalid path %s", __func__, path);
return -1;
}
// is r allowed to descend under the parent dir?
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_error("%s: pid %d (uid %u gid %u) may not read under %s", __func__,
r.pid, r.uid, r.gid, path);
return -2;
}
// does r have privilege over the cgroup dir?
if (!may_access(r.pid, r.uid, r.gid, path, O_RDWR)) {
nih_error("%s: Pid %d may not chown %s\n", __func__, r.pid, path);
return -2;
}
// go ahead and chown it.
if (!chown_cgroup_path(path, v.uid, v.gid, false)) {
nih_error("%s: Failed to change ownership on %s to %u:%u", __func__,
path, v.uid, v.gid);
return -2;
}
if (is_unified_controller(controller)) {
NIH_MUST( nih_strcat(&path, NULL, U_LEAF) );
if (dir_exists(path) && !chown_cgroup_path(path, v.uid, v.gid, false)) {
nih_warn("%s: Failed to chown leaf directory for %s to %u:%u",
__func__, path, v.uid, v.gid);
return -2;
}
}
return 0;
}
int chown_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, struct ucred v)
{
uid_t uid;
nih_local char *c = NULL;
char *tok;
int ret;
/* If caller is not root in his userns, then he can't chown, as
* that requires privilege over two uids */
if (r.uid) {
if (!hostuid_to_ns(r.uid, r.pid, &uid) || uid != 0) {
nih_error("%s: Chown requested by non-root uid %u", __func__, r.uid);
return -1;
}
}
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (strcmp(controller, "all") != 0 && !strchr(controller, ','))
return do_chown_main(controller, cgroup, p, r, v);
if (strcmp(controller, "all") == 0) {
if (!all_controllers)
return 0;
c = NIH_MUST( nih_strdup(NULL, all_controllers) );
} else {
c = NIH_MUST( nih_strdup(NULL, controller) );
do_prune_comounts(c);
}
tok = strtok(c, ",");
while (tok) {
ret = do_chown_main(tok, cgroup, p, r, v);
if (ret == -2) // permission denied - ignore for group requests
goto next;
if (ret != 0)
return -1;
next:
tok = strtok(NULL, ",");
}
return 0;
}
int do_chmod_main(const char *controller, const char *cgroup, const char *file,
struct ucred p, struct ucred r, int mode)
{
char rcgpath[MAXPATHLEN];
nih_local char *path = NULL;
// Get r's current cgroup in rcgpath
if (!compute_pid_cgroup(r.pid, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
path = NIH_MUST( nih_sprintf(NULL, "%s/%s", rcgpath, cgroup) );
if (file && strlen(file))
NIH_MUST( nih_strcat_sprintf(&path, NULL, "/%s", file) );
if (realpath_escapes(path, rcgpath)) {
nih_error("%s: Invalid path %s", __func__, path);
return -1;
}
// is r allowed to descend under the parent dir?
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_error("%s: pid %d (uid %u gid %u) may not read under %s", __func__,
r.pid, r.uid, r.gid, path);
return -2;
}
// does r have privilege over the cgroup dir?
if (!may_access(r.pid, r.uid, r.gid, path, O_RDWR)) {
nih_error("%s: Pid %d may not chmod %s\n", __func__, r.pid, path);
return -2;
}
// go ahead and chmod it.
if (!chmod_cgroup_path(path, mode)) {
nih_error("%s: Failed to change mode on %s to %d", __func__,
path, mode);
return -2;
}
return 0;
}
int chmod_main(const char *controller, const char *cgroup, const char *file,
struct ucred p, struct ucred r, int mode)
{
nih_local char *c = NULL;
char *tok;
int ret;
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (file && ( strchr(file, '/') || strchr(file, '\\')) ) {
nih_error("%s: invalid file", __func__);
return -1;
}
if (strcmp(controller, "all") != 0 && !strchr(controller, ','))
return do_chmod_main(controller, cgroup, file, p, r, mode);
if (strcmp(controller, "all") == 0) {
if (!all_controllers)
return 0;
c = NIH_MUST( nih_strdup(NULL, all_controllers) );
} else {
c = NIH_MUST( nih_strdup(NULL, controller) );
do_prune_comounts(c);
}
tok = strtok(c, ",");
while (tok) {
ret = do_chmod_main(tok, cgroup, file, p, r, mode);
if (ret == -2) // permission denied - ignore for group requests
goto next;
if (ret != 0)
return -1;
next:
tok = strtok(NULL, ",");
}
return 0;
}
int get_value_main(void *parent, char *controller, const char *cgroup,
const char *key, struct ucred p, struct ucred r, char **value)
{
char path[MAXPATHLEN];
if (!(prune_verify_comounts(controller))) {
nih_error("%s: Multiple controllers given: %s",
__func__, controller);
return -1;
}
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
nih_error("%s: Could not determine the requested cgroup (%s:%s)",
__func__, controller, cgroup);
return -1;
}
/* Check access rights to the cgroup directory */
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_debug("%s: Pid %d may not access %s\n", __func__, r.pid, path);
return -1;
}
if (!path_is_under_proxycg(p.pid, controller, path)) {
nih_debug("%s: target cgroup is not below r (%d)'s", __func__,
r.pid);
return -1;
}
/* append the filename */
if (strlen(path) + strlen(key) + 2 > MAXPATHLEN) {
nih_error("%s: filename too long for cgroup %s key %s", __func__, path, key);
return -1;
}
strncat(path, "/", MAXPATHLEN-1);
strncat(path, key, MAXPATHLEN-1);
/* Check access rights to the file itself */
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_debug("%s: Pid %d may not access %s\n", __func__, r.pid, path);
return -1;
}
/* read and return the value */
*value = file_read_string(parent, path);
if (!*value) {
nih_error("%s: Failed to read value from %s", __func__, path);
return -1;
}
nih_info(_("Sending to client: %s"), *value);
return 0;
}
int set_value_main(char *controller, const char *cgroup,
const char *key, const char *value, struct ucred p,
struct ucred r)
{
char path[MAXPATHLEN];
if (!(prune_verify_comounts(controller))) {
nih_error("%s: Multiple controllers given: %s",
__func__, controller);
return -1;
}
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
nih_error("%s: Could not determine the requested cgroup (%s:%s)",
__func__, controller, cgroup);
return -1;
}
if (!path_is_under_proxycg(p.pid, controller, path)) {
nih_debug("%s: target cgroup is not below r (%d)'s", __func__,
r.pid);
return -1;
}
/* Check access rights to the cgroup directory */
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_debug("%s: Pid %d may not access %s\n", __func__, r.pid, path);
return -1;
}
/* append the filename */
if (strlen(path) + strlen(key) + 2 > MAXPATHLEN) {
nih_error("%s: filename too long for cgroup %s key %s", __func__, path, key);
return -1;
}
strncat(path, "/", MAXPATHLEN-1);
strncat(path, key, MAXPATHLEN-1);
/* Check access rights to the file itself */
if (!may_access(r.pid, r.uid, r.gid, path, O_WRONLY)) {
nih_debug("%s: Pid %d may not access %s\n", __func__, r.pid, path);
return -1;
}
/* read and return the value */
if (!set_value(controller, path, value)) {
nih_error("%s: Failed to set value %s to %s", __func__, path, value);
return -1;
}
return 0;
}
/*
* Refuse any '..', and consolidate any '//'
*/
static bool normalize_path(char *path)
{
if (strstr(path, ".."))
return false;
while ((path = strstr(path, "//")) != NULL) {
char *p2 = path+1;
while (*p2 == '/')
p2++;
memmove(path, p2, strlen(p2)+1);
path++;
}
return true;
}
/*
* Recursively delete a cgroup.
* Cgroup files can't be deleted, but are cleaned up when you remove the
* containing directory. A directory cannot be removed until all its
* children are removed, and can't be removed if any tasks remain.
*
* We allow any task which may write under /a/b to delete any cgroups
* under that, even if, say, it technically is not allowed to remove
* /a/b/c/d/.
*/
static int recursive_rmdir(char *path)
{
struct dirent dirent, *direntp;
DIR *dir;
int failed = 0;
dir = opendir(path);
if (!dir) {
nih_error("%s: Failed to open dir %s for recursive deletion", __func__, path);
return -1;
}
while (!readdir_r(dir, &dirent, &direntp)) {
struct stat mystat;
nih_local char *pathname = NULL;
int rc;
if (!direntp)
break;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
pathname = NIH_MUST( nih_sprintf(NULL, "%s/%s", path, direntp->d_name) );
rc = lstat(pathname, &mystat);
if (rc) {
failed = 1;
continue;
}
if (S_ISDIR(mystat.st_mode)) {
if (recursive_rmdir(pathname) < 0)
failed = 1;
}
}
if (closedir(dir) < 0)
failed = 1;
if (rmdir(path) < 0)
failed = 1;
return failed ? -1 : 0;
}
int do_remove_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, int recursive, int32_t *existed)
{
char rcgpath[MAXPATHLEN];
size_t cgroup_len;
nih_local char *working = NULL, *copy = NULL, *wcgroup = NULL;
char *p1;
*existed = 1;
// Get r's current cgroup in rcgpath
if (!compute_pid_cgroup(r.pid, controller, "", rcgpath, NULL)) {
nih_error("%s: Could not determine the requestor's cgroup for %s",
__func__, controller);
return -1;
}
cgroup_len = strlen(cgroup);
if (strlen(rcgpath) + cgroup_len > MAXPATHLEN) {
nih_error("%s: Path name too long", __func__);
return -1;
}
wcgroup = NIH_MUST( nih_strdup(NULL, cgroup) );
if (!normalize_path(wcgroup))
return -1;
working = NIH_MUST( nih_strdup(NULL, rcgpath) );
NIH_MUST( nih_strcat(&working, NULL, "/") );
NIH_MUST( nih_strcat(&working, NULL, wcgroup) );
if (!dir_exists(working)) {
*existed = -1;
return 0;
}
// must have write access to the parent dir
copy = NIH_MUST( nih_strdup(NULL, working) );
if (!(p1 = strrchr(copy, '/')))
return -1;
*p1 = '\0';
if (!may_access(r.pid, r.uid, r.gid, copy, O_WRONLY)) {
nih_error("%s: pid %d (%u:%u) may not remove %s", __func__,
r.pid, r.uid, r.gid, copy);
return -2;
}
if (is_unified_controller(controller)) {
nih_local char *fpath = NULL;
fpath = NIH_MUST( nih_sprintf(NULL, "%s%s", working, U_LEAF) );
if (rmdir(fpath) < 0) {
nih_error("%s: Failed to remove %s: %s", __func__, fpath, strerror(errno));
return errno == EPERM ? -2 : -1;
}
}
if (!recursive) {
if (rmdir(working) < 0) {
nih_error("%s: Failed to remove %s: %s", __func__, working, strerror(errno));
return errno == EPERM ? -2 : -1;
}
} else if (recursive_rmdir(working) < 0)
return -1;
nih_info(_("Removed %s for %d (%u:%u)"), working, r.pid,
r.uid, r.gid);
return 0;
}
int remove_main(const char *controller, const char *cgroup, struct ucred p,
struct ucred r, int recursive, int32_t *existed)
{
nih_local char *c = NULL;
char *tok;
int ret;
*existed = 1;
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (strcmp(controller, "all") != 0 && !strchr(controller, ','))
return do_remove_main(controller, cgroup, p, r, recursive, existed);
if (strcmp(controller, "all") == 0) {
if (!all_controllers)
return 0;
c = NIH_MUST( nih_strdup(NULL, all_controllers) );
} else {
c = NIH_MUST( nih_strdup(NULL, controller) );
do_prune_comounts(c);
}
tok = strtok(c, ",");
while (tok) {
int32_t e = 1;
ret = do_remove_main(tok, cgroup, p, r, recursive, &e);
if (ret == -2) // permission denied - ignore for group requests
goto next;
if (ret != 0)
return -1;
if (!e)
*existed = 0;
next:
tok = strtok(NULL, ",");
}
return 0;
}
int get_tasks_main(void *parent, char *controller, const char *cgroup,
struct ucred p, struct ucred r, int32_t **pids)
{
char path[MAXPATHLEN];
const char *key = "tasks";
int alloced_pids = 0, nrpids = 0;
if (!sane_cgroup(cgroup)) {
nih_error("%s: unsafe cgroup", __func__);
return -1;
}
if (!(prune_verify_comounts(controller))) {
nih_error("%s: Multiple controllers given: %s",
__func__, controller);
return -1;
}
if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
nih_error("%s: Could not determine the requested cgroup (%s:%s)",
__func__, controller, cgroup);
return -1;
}
if (!path_is_under_proxycg(p.pid, controller, path)) {
nih_debug("%s: target cgroup is not below r (%d)'s", __func__,
r.pid);
return -1;
}
/* Check access rights to the cgroup directory */
if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
nih_debug("%s: Pid %d may not access %s\n", __func__, r.pid, path);
return -1;
}
/* append the filename */
if (strlen(path) + strlen(key) + 2 > MAXPATHLEN) {
nih_error("%s: filename too long for cgroup %s key %s", __func__, path, key);
return -1;
}
strncat(path, "/", MAXPATHLEN-1);
strncat(path, key, MAXPATHLEN-1);
*pids = NULL;
if (file_read_pids(parent, path, pids, &alloced_pids, &nrpids) < 0) {
if (*pids)
nih_free(*pids);
return -1;
}
return nrpids;
}
int do_collect_tasks(void *parent, char **path, int32_t **pids,
int *alloced_pids, int *nrpids)
{
struct dirent dirent, *direntp;
DIR *dir;
const char *key = "tasks";
dir = opendir(*path);
if (!dir) {
nih_warn("%s: Failed to open dir %s for recursive deletion", __func__, *path);
return -2;
}
while (!readdir_r(dir, &dirent, &direntp)) {
struct stat mystat;
nih_local char *childname = NULL;
int rc;
if (!direntp)
break;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
childname = NIH_MUST( nih_sprintf(NULL, "%s/%s", *path, direntp->d_name) );
rc = lstat(childname, &mystat);
if (rc)
continue;
if (S_ISDIR(mystat.st_mode))
if (do_collect_tasks(parent, &childname, pids,
alloced_pids, nrpids) == -1)
nih_info("%s: error descending subdirs", __func__);
}
closedir(dir);
/* Get tasks for this directory */
NIH_MUST( nih_strcat_sprintf(path, NULL, "/%s", key) );
return file_read_pids(parent, *path, pids, alloced_pids, nrpids);
}
int collect_tasks(void *parent, const char *controller, const char *cgroup,
struct ucred p, struct ucred r, int32_t **pids,
int *alloced_pids, int *nrpids)
{
char path[MAXPATHLEN];
nih_local char *rpath = NULL;