-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxcfs.c
3212 lines (2779 loc) · 68 KB
/
lxcfs.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
/* lxcfs
*
* Copyright © 2014,2015 Canonical, Inc
* Author: Serge Hallyn <[email protected]>
*
* See COPYING file for details.
*/
/*
* TODO XXX
* sanitize paths for '..', cgmanager's not doing that for us any more
* does fuse help us?
* Surely there are more paths we'll need to sanitize - look back through
* cgmanager's sources.
*/
#define FUSE_USE_VERSION 26
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <fuse.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <wait.h>
#ifdef FORTRAVIS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include <glib-object.h>
#endif
#include "cgfs.h"
#include "config.h" // for VERSION
enum {
LXC_TYPE_CGDIR,
LXC_TYPE_CGFILE,
LXC_TYPE_PROC_MEMINFO,
LXC_TYPE_PROC_CPUINFO,
LXC_TYPE_PROC_UPTIME,
LXC_TYPE_PROC_STAT,
LXC_TYPE_PROC_DISKSTATS,
};
struct file_info {
char *controller;
char *cgroup;
char *file;
int type;
char *buf; // unused as of yet
int buflen;
int size; //actual data size
int cached;
};
/* reserve buffer size, for cpuall in /proc/stat */
#define BUF_RESERVE_SIZE 256
/*
* append pid to *src.
* src: a pointer to a char* in which ot append the pid.
* sz: the number of characters printed so far, minus trailing \0.
* asz: the allocated size so far
* pid: the pid to append
*/
static void must_strcat_pid(char **src, size_t *sz, size_t *asz, pid_t pid)
{
char *d = *src;
char tmp[30];
sprintf(tmp, "%d\n", (int)pid);
if (!d) {
do {
d = malloc(BUF_RESERVE_SIZE);
} while (!d);
*src = d;
*asz = BUF_RESERVE_SIZE;
} else if (strlen(tmp) + sz + 1 >= asz) {
do {
d = realloc(d, *asz + BUF_RESERVE_SIZE);
} while (!d);
*src = d;
*asz += BUF_RESERVE_SIZE;
}
memcpy(d+*sz, tmp, strlen(tmp));
*sz += strlen(tmp);
d[*sz] = '\0';
}
static int wait_for_pid(pid_t pid)
{
int status, ret;
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
return -1;
return 0;
}
/*
* Given a open file * to /proc/pid/{u,g}id_map, and an id
* valid in the caller's namespace, return the id mapped into
* pid's namespace.
* Returns the mapped id, or -1 on error.
*/
unsigned int
convert_id_to_ns(FILE *idfile, unsigned int in_id)
{
unsigned int nsuid, // base id for a range in the idfile's namespace
hostuid, // base id for a range in the caller's namespace
count; // number of ids in this range
char line[400];
int ret;
fseek(idfile, 0L, SEEK_SET);
while (fgets(line, 400, idfile)) {
ret = sscanf(line, "%u %u %u\n", &nsuid, &hostuid, &count);
if (ret != 3)
continue;
if (hostuid + count < hostuid || nsuid + count < nsuid) {
/*
* uids wrapped around - unexpected as this is a procfile,
* so just bail.
*/
fprintf(stderr, "pid wrapparound at entry %u %u %u in %s\n",
nsuid, hostuid, count, line);
return -1;
}
if (hostuid <= in_id && hostuid+count > in_id) {
/*
* now since hostuid <= in_id < hostuid+count, and
* hostuid+count and nsuid+count do not wrap around,
* we know that nsuid+(in_id-hostuid) which must be
* less that nsuid+(count) must not wrap around
*/
return (in_id - hostuid) + nsuid;
}
}
// no answer found
return -1;
}
/*
* for is_privileged_over,
* specify whether we require the calling uid to be root in his
* namespace
*/
#define NS_ROOT_REQD true
#define NS_ROOT_OPT false
#define PROCLEN 100
static bool is_privileged_over(pid_t pid, uid_t uid, uid_t victim, bool req_ns_root)
{
char fpath[PROCLEN];
int ret;
bool answer = false;
uid_t nsuid;
if (victim == -1 || uid == -1)
return false;
/*
* If the request is one not requiring root in the namespace,
* then having the same uid suffices. (i.e. uid 1000 has write
* access to files owned by uid 1000
*/
if (!req_ns_root && uid == victim)
return true;
ret = snprintf(fpath, PROCLEN, "/proc/%d/uid_map", pid);
if (ret < 0 || ret >= PROCLEN)
return false;
FILE *f = fopen(fpath, "r");
if (!f)
return false;
/* if caller's not root in his namespace, reject */
nsuid = convert_id_to_ns(f, uid);
if (nsuid)
goto out;
/*
* If victim is not mapped into caller's ns, reject.
* XXX I'm not sure this check is needed given that fuse
* will be sending requests where the vfs has converted
*/
nsuid = convert_id_to_ns(f, victim);
if (nsuid == -1)
goto out;
answer = true;
out:
fclose(f);
return answer;
}
static bool perms_include(int fmode, mode_t req_mode)
{
mode_t r;
switch (req_mode & O_ACCMODE) {
case O_RDONLY:
r = S_IROTH;
break;
case O_WRONLY:
r = S_IWOTH;
break;
case O_RDWR:
r = S_IROTH | S_IWOTH;
break;
default:
return false;
}
return ((fmode & r) == r);
}
/*
* taskcg is a/b/c
* querycg is /a/b/c/d/e
* we return 'd'
*/
static char *get_next_cgroup_dir(const char *taskcg, const char *querycg)
{
char *start, *end;
if (strlen(taskcg) <= strlen(querycg)) {
fprintf(stderr, "%s: I was fed bad input\n", __func__);
return NULL;
}
if (strcmp(querycg, "/") == 0)
start = strdup(taskcg + 1);
else
start = strdup(taskcg + strlen(querycg) + 1);
if (!start)
return NULL;
end = strchr(start, '/');
if (end)
*end = '\0';
return start;
}
static void stripnewline(char *x)
{
size_t l = strlen(x);
if (l && x[l-1] == '\n')
x[l-1] = '\0';
}
static char *get_pid_cgroup(pid_t pid, const char *contrl)
{
char fnam[PROCLEN];
FILE *f;
char *answer = NULL;
char *line = NULL;
size_t len = 0;
int ret;
const char *h = find_mounted_controller(contrl);
if (!h)
return NULL;
ret = snprintf(fnam, PROCLEN, "/proc/%d/cgroup", pid);
if (ret < 0 || ret >= PROCLEN)
return NULL;
if (!(f = fopen(fnam, "r")))
return NULL;
while (getline(&line, &len, f) != -1) {
char *c1, *c2;
if (!line[0])
continue;
c1 = strchr(line, ':');
if (!c1)
goto out;
c1++;
c2 = strchr(c1, ':');
if (!c2)
goto out;
*c2 = '\0';
if (strcmp(c1, h) != 0)
continue;
c2++;
stripnewline(c2);
do {
answer = strdup(c2);
} while (!answer);
break;
}
out:
fclose(f);
free(line);
return answer;
}
/*
* check whether a fuse context may access a cgroup dir or file
*
* If file is not null, it is a cgroup file to check under cg.
* If file is null, then we are checking perms on cg itself.
*
* For files we can check the mode of the list_keys result.
* For cgroups, we must make assumptions based on the files under the
* cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
* yet.
*/
static bool fc_may_access(struct fuse_context *fc, const char *contrl, const char *cg, const char *file, mode_t mode)
{
struct cgfs_files *k = NULL;
bool ret = false;
if (!file)
file = "tasks";
if (*file == '/')
file++;
k = cgfs_get_key(contrl, cg, file);
if (!k)
return false;
if (is_privileged_over(fc->pid, fc->uid, k->uid, NS_ROOT_OPT)) {
if (perms_include(k->mode >> 6, mode)) {
ret = true;
goto out;
}
}
if (fc->gid == k->gid) {
if (perms_include(k->mode >> 3, mode)) {
ret = true;
goto out;
}
}
ret = perms_include(k->mode, mode);
out:
free_key(k);
return ret;
}
#define INITSCOPE "/init.scope"
static void prune_init_slice(char *cg)
{
char *point;
point = cg + strlen(cg) - strlen(INITSCOPE);
if (point < cg)
return;
if (strcmp(point, INITSCOPE) == 0) {
if (point == cg)
*(point+1) = '\0';
else
*point = '\0';
}
}
/*
* If caller is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
* If caller is in /a, he may act on /a/b, but not on /b.
* if the answer is false and nextcg is not NULL, then *nextcg will point
* to a string containing the next cgroup directory under cg, which must be
* freed by the caller.
*/
static bool caller_is_in_ancestor(pid_t pid, const char *contrl, const char *cg, char **nextcg)
{
bool answer = false;
char *c2 = get_pid_cgroup(pid, contrl);
char *linecmp;
if (!c2)
return false;
prune_init_slice(c2);
/*
* callers pass in '/' for root cgroup, otherwise they pass
* in a cgroup without leading '/'
*/
linecmp = *cg == '/' ? c2 : c2+1;
if (strncmp(linecmp, cg, strlen(linecmp)) != 0) {
if (nextcg) {
*nextcg = get_next_cgroup_dir(linecmp, cg);
}
goto out;
}
answer = true;
out:
free(c2);
return answer;
}
/*
* If caller is in /a/b/c, he may see that /a exists, but not /b or /a/c.
*/
static bool caller_may_see_dir(pid_t pid, const char *contrl, const char *cg)
{
bool answer = false;
char *c2, *task_cg;
size_t target_len, task_len;
if (strcmp(cg, "/") == 0)
return true;
c2 = get_pid_cgroup(pid, contrl);
if (!c2)
return false;
prune_init_slice(c2);
task_cg = c2 + 1;
target_len = strlen(cg);
task_len = strlen(task_cg);
if (task_len == 0) {
/* Task is in the root cg, it can see everything. This case is
* not handled by the strmcps below, since they test for the
* last /, but that is the first / that we've chopped off
* above.
*/
answer = true;
goto out;
}
if (strcmp(cg, task_cg) == 0) {
answer = true;
goto out;
}
if (target_len < task_len) {
/* looking up a parent dir */
if (strncmp(task_cg, cg, target_len) == 0 && task_cg[target_len] == '/')
answer = true;
goto out;
}
if (target_len > task_len) {
/* looking up a child dir */
if (strncmp(task_cg, cg, task_len) == 0 && cg[task_len] == '/')
answer = true;
goto out;
}
out:
free(c2);
return answer;
}
/*
* given /cgroup/freezer/a/b, return "freezer".
* the returned char* should NOT be freed.
*/
static char *pick_controller_from_path(struct fuse_context *fc, const char *path)
{
const char *p1;
char *contr, *slash;
if (strlen(path) < 9)
return NULL;
if (*(path+7) != '/')
return NULL;
p1 = path+8;
contr = strdupa(p1);
if (!contr)
return NULL;
slash = strstr(contr, "/");
if (slash)
*slash = '\0';
int i;
for (i = 0; i < num_hierarchies; i++) {
if (hierarchies[i] && strcmp(hierarchies[i], contr) == 0)
return hierarchies[i];
}
return NULL;
}
/*
* Find the start of cgroup in /cgroup/controller/the/cgroup/path
* Note that the returned value may include files (keynames) etc
*/
static const char *find_cgroup_in_path(const char *path)
{
const char *p1;
if (strlen(path) < 9)
return NULL;
p1 = strstr(path+8, "/");
if (!p1)
return NULL;
return p1+1;
}
/*
* dir should be freed, file not
*/
static void get_cgdir_and_path(const char *cg, char **dir, char **file)
{
char *p;
do {
*dir = strdup(cg);
} while (!*dir);
*file = strrchr(cg, '/');
if (!*file) {
*file = NULL;
return;
}
p = strrchr(*dir, '/');
*p = '\0';
}
/*
* FUSE ops for /cgroup
*/
static int cg_getattr(const char *path, struct stat *sb)
{
struct timespec now;
struct fuse_context *fc = fuse_get_context();
char * cgdir = NULL;
char *fpath = NULL, *path1, *path2;
struct cgfs_files *k = NULL;
const char *cgroup;
const char *controller = NULL;
int ret = -ENOENT;
if (!fc)
return -EIO;
memset(sb, 0, sizeof(struct stat));
if (clock_gettime(CLOCK_REALTIME, &now) < 0)
return -EINVAL;
sb->st_uid = sb->st_gid = 0;
sb->st_atim = sb->st_mtim = sb->st_ctim = now;
sb->st_size = 0;
if (strcmp(path, "/cgroup") == 0) {
sb->st_mode = S_IFDIR | 00755;
sb->st_nlink = 2;
return 0;
}
controller = pick_controller_from_path(fc, path);
if (!controller)
return -EIO;
cgroup = find_cgroup_in_path(path);
if (!cgroup) {
/* this is just /cgroup/controller, return it as a dir */
sb->st_mode = S_IFDIR | 00755;
sb->st_nlink = 2;
return 0;
}
get_cgdir_and_path(cgroup, &cgdir, &fpath);
if (!fpath) {
path1 = "/";
path2 = cgdir;
} else {
path1 = cgdir;
path2 = fpath;
}
/* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
* Then check that caller's cgroup is under path if fpath is a child
* cgroup, or cgdir if fpath is a file */
if (is_child_cgroup(controller, path1, path2)) {
if (!caller_may_see_dir(fc->pid, controller, cgroup)) {
ret = -ENOENT;
goto out;
}
if (!caller_is_in_ancestor(fc->pid, controller, cgroup, NULL)) {
/* this is just /cgroup/controller, return it as a dir */
sb->st_mode = S_IFDIR | 00555;
sb->st_nlink = 2;
ret = 0;
goto out;
}
if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY)) {
ret = -EACCES;
goto out;
}
// get uid, gid, from '/tasks' file and make up a mode
// That is a hack, until cgmanager gains a GetCgroupPerms fn.
sb->st_mode = S_IFDIR | 00755;
k = cgfs_get_key(controller, cgroup, "tasks");
if (!k) {
sb->st_uid = sb->st_gid = 0;
} else {
sb->st_uid = k->uid;
sb->st_gid = k->gid;
}
free_key(k);
sb->st_nlink = 2;
ret = 0;
goto out;
}
if ((k = cgfs_get_key(controller, path1, path2)) != NULL) {
sb->st_mode = S_IFREG | k->mode;
sb->st_nlink = 1;
sb->st_uid = k->uid;
sb->st_gid = k->gid;
sb->st_size = 0;
free_key(k);
if (!caller_is_in_ancestor(fc->pid, controller, path1, NULL)) {
ret = -ENOENT;
goto out;
}
if (!fc_may_access(fc, controller, path1, path2, O_RDONLY)) {
ret = -EACCES;
goto out;
}
ret = 0;
}
out:
free(cgdir);
return ret;
}
static int cg_opendir(const char *path, struct fuse_file_info *fi)
{
struct fuse_context *fc = fuse_get_context();
const char *cgroup;
struct file_info *dir_info;
char *controller = NULL;
if (!fc)
return -EIO;
if (strcmp(path, "/cgroup") == 0) {
cgroup = NULL;
controller = NULL;
} else {
// return list of keys for the controller, and list of child cgroups
controller = pick_controller_from_path(fc, path);
if (!controller)
return -EIO;
cgroup = find_cgroup_in_path(path);
if (!cgroup) {
/* this is just /cgroup/controller, return its contents */
cgroup = "/";
}
}
if (cgroup) {
if (!caller_may_see_dir(fc->pid, controller, cgroup))
return -ENOENT;
if (!fc_may_access(fc, controller, cgroup, NULL, O_RDONLY))
return -EACCES;
}
/* we'll free this at cg_releasedir */
dir_info = malloc(sizeof(*dir_info));
if (!dir_info)
return -ENOMEM;
dir_info->controller = must_copy_string(controller);
dir_info->cgroup = must_copy_string(cgroup);
dir_info->type = LXC_TYPE_CGDIR;
dir_info->buf = NULL;
dir_info->file = NULL;
dir_info->buflen = 0;
fi->fh = (unsigned long)dir_info;
return 0;
}
static int cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi)
{
struct file_info *d = (struct file_info *)fi->fh;
struct cgfs_files **list = NULL;
int i, ret;
char *nextcg = NULL;
struct fuse_context *fc = fuse_get_context();
char **clist = NULL;
if (d->type != LXC_TYPE_CGDIR) {
fprintf(stderr, "Internal error: file cache info used in readdir\n");
return -EIO;
}
if (!d->cgroup && !d->controller) {
// ls /var/lib/lxcfs/cgroup - just show list of controllers
int i;
for (i = 0; i < num_hierarchies; i++) {
if (hierarchies[i] && filler(buf, hierarchies[i], NULL, 0) != 0) {
return -EIO;
}
}
return 0;
}
if (!cgfs_list_keys(d->controller, d->cgroup, &list)) {
// not a valid cgroup
ret = -EINVAL;
goto out;
}
if (!caller_is_in_ancestor(fc->pid, d->controller, d->cgroup, &nextcg)) {
if (nextcg) {
int ret;
ret = filler(buf, nextcg, NULL, 0);
free(nextcg);
if (ret != 0) {
ret = -EIO;
goto out;
}
}
ret = 0;
goto out;
}
for (i = 0; list[i]; i++) {
if (filler(buf, list[i]->name, NULL, 0) != 0) {
ret = -EIO;
goto out;
}
}
// now get the list of child cgroups
if (!cgfs_list_children(d->controller, d->cgroup, &clist)) {
ret = 0;
goto out;
}
for (i = 0; clist[i]; i++) {
if (filler(buf, clist[i], NULL, 0) != 0) {
ret = -EIO;
goto out;
}
}
ret = 0;
out:
free_keys(list);
if (clist) {
for (i = 0; clist[i]; i++)
free(clist[i]);
free(clist);
}
return ret;
}
static void do_release_file_info(struct file_info *f)
{
if (!f)
return;
free(f->controller);
free(f->cgroup);
free(f->file);
free(f->buf);
free(f);
}
static int cg_releasedir(const char *path, struct fuse_file_info *fi)
{
struct file_info *d = (struct file_info *)fi->fh;
do_release_file_info(d);
return 0;
}
static int cg_open(const char *path, struct fuse_file_info *fi)
{
const char *cgroup;
char *fpath = NULL, *path1, *path2, * cgdir = NULL, *controller;
struct cgfs_files *k = NULL;
struct file_info *file_info;
struct fuse_context *fc = fuse_get_context();
int ret;
if (!fc)
return -EIO;
controller = pick_controller_from_path(fc, path);
if (!controller)
return -EIO;
cgroup = find_cgroup_in_path(path);
if (!cgroup)
return -EINVAL;
get_cgdir_and_path(cgroup, &cgdir, &fpath);
if (!fpath) {
path1 = "/";
path2 = cgdir;
} else {
path1 = cgdir;
path2 = fpath;
}
k = cgfs_get_key(controller, path1, path2);
if (!k) {
ret = -EINVAL;
goto out;
}
free_key(k);
if (!caller_may_see_dir(fc->pid, controller, path1)) {
ret = -ENOENT;
goto out;
}
if (!fc_may_access(fc, controller, path1, path2, fi->flags)) {
// should never get here
ret = -EACCES;
goto out;
}
/* we'll free this at cg_release */
file_info = malloc(sizeof(*file_info));
if (!file_info) {
ret = -ENOMEM;
goto out;
}
file_info->controller = must_copy_string(controller);
file_info->cgroup = must_copy_string(path1);
file_info->file = must_copy_string(path2);
file_info->type = LXC_TYPE_CGFILE;
file_info->buf = NULL;
file_info->buflen = 0;
fi->fh = (unsigned long)file_info;
ret = 0;
out:
free(cgdir);
return ret;
}
static int cg_release(const char *path, struct fuse_file_info *fi)
{
struct file_info *f = (struct file_info *)fi->fh;
do_release_file_info(f);
return 0;
}
static int msgrecv(int sockfd, void *buf, size_t len)
{
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
tv.tv_sec = 2;
tv.tv_usec = 0;
if (select(sockfd+1, &rfds, NULL, NULL, &tv) <= 0)
return -1;
return recv(sockfd, buf, len, MSG_DONTWAIT);
}
#define SEND_CREDS_OK 0
#define SEND_CREDS_NOTSK 1
#define SEND_CREDS_FAIL 2
static int send_creds(int sock, struct ucred *cred, char v, bool pingfirst)
{
struct msghdr msg = { 0 };
struct iovec iov;
struct cmsghdr *cmsg;
char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
char buf[1];
buf[0] = 'p';
if (pingfirst) {
if (msgrecv(sock, buf, 1) != 1) {
fprintf(stderr, "%s: Error getting reply from server over socketpair\n",
__func__);
return SEND_CREDS_FAIL;
}
}
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_CREDENTIALS;
memcpy(CMSG_DATA(cmsg), cred, sizeof(*cred));
msg.msg_name = NULL;
msg.msg_namelen = 0;
buf[0] = v;
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (sendmsg(sock, &msg, 0) < 0) {
fprintf(stderr, "%s: failed at sendmsg: %s\n", __func__,
strerror(errno));
if (errno == 3)
return SEND_CREDS_NOTSK;
return SEND_CREDS_FAIL;
}
return SEND_CREDS_OK;
}
static bool recv_creds(int sock, struct ucred *cred, char *v)
{
struct msghdr msg = { 0 };
struct iovec iov;
struct cmsghdr *cmsg;
char cmsgbuf[CMSG_SPACE(sizeof(*cred))];
char buf[1];
int ret;
int optval = 1;
struct timeval tv;
fd_set rfds;
*v = '1';
cred->pid = -1;
cred->uid = -1;
cred->gid = -1;
if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1) {
fprintf(stderr, "Failed to set passcred: %s\n", strerror(errno));
return false;
}
buf[0] = '1';
if (write(sock, buf, 1) != 1) {
fprintf(stderr, "Failed to start write on scm fd: %s\n", strerror(errno));
return false;
}
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
tv.tv_sec = 2;
tv.tv_usec = 0;
if (select(sock+1, &rfds, NULL, NULL, &tv) <= 0) {
fprintf(stderr, "Failed to select for scm_cred: %s\n",
strerror(errno));
return false;
}
ret = recvmsg(sock, &msg, MSG_DONTWAIT);
if (ret < 0) {
fprintf(stderr, "Failed to receive scm_cred: %s\n",
strerror(errno));
return false;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) &&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_CREDENTIALS) {
memcpy(cred, CMSG_DATA(cmsg), sizeof(*cred));
}
*v = buf[0];
return true;
}
/*
* pid_to_ns - reads pids from a ucred over a socket, then writes the
* int value back over the socket. This shifts the pid from the
* sender's pidns into tpid's pidns.
*/
static void pid_to_ns(int sock, pid_t tpid)
{
char v = '0';