]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/cgroup_fuse.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
11 #define _FILE_OFFSET_BITS 64
13 #define __STDC_FORMAT_MACROS
31 #include <linux/magic.h>
32 #include <linux/sched.h>
33 #include <sys/epoll.h>
35 #include <sys/mount.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/syscall.h>
39 #include <sys/sysinfo.h>
44 #include "cgroups/cgroup.h"
45 #include "cgroups/cgroup_utils.h"
46 #include "memory_utils.h"
55 struct pid_ns_clone_args
{
59 /* pid_from_ns or pid_to_ns. */
60 int (*wrapped
) (int, pid_t
);
64 * given /cgroup/freezer/a/b, return "freezer".
65 * the returned char* should NOT be freed.
67 static char *pick_controller_from_path(struct fuse_context
*fc
, const char *path
)
72 if (strlen(path
) < 9) {
76 if (*(path
+ 7) != '/') {
86 slash
= strstr(contr
, "/");
90 for (struct hierarchy
**h
= cgroup_ops
->hierarchies
; h
&& *h
; h
++) {
91 if ((*h
)->__controllers
&& strcmp((*h
)->__controllers
, contr
) == 0)
92 return (*h
)->__controllers
;
99 * Find the start of cgroup in /cgroup/controller/the/cgroup/path
100 * Note that the returned value may include files (keynames) etc
102 static const char *find_cgroup_in_path(const char *path
)
106 if (strlen(path
) < 9) {
110 p1
= strstr(path
+ 8, "/");
120 * split the last path element from the path in @cg.
121 * @dir is newly allocated and should be freed, @last not
123 static void get_cgdir_and_path(const char *cg
, char **dir
, char **last
)
130 *last
= strrchr(cg
, '/');
135 p
= strrchr(*dir
, '/');
139 static bool is_child_cgroup(const char *controller
, const char *cgroup
, const char *f
)
147 cfd
= get_cgroup_fd(controller
);
151 /* Make sure we pass a relative path to *at() family of functions.
152 * . + /cgroup + / + f + \0
154 len
= strlen(cgroup
) + strlen(f
) + 3;
156 ret
= snprintf(fnam
, len
, "%s%s/%s", dot_or_empty(cgroup
), cgroup
, f
);
157 if (ret
< 0 || (size_t)ret
>= len
)
160 ret
= fstatat(cfd
, fnam
, &sb
, 0);
161 if (ret
< 0 || !S_ISDIR(sb
.st_mode
))
168 * If pid is in /a/b/c, he may see that /a exists, but not /b or /a/c.
170 static bool caller_may_see_dir(pid_t pid
, const char *contrl
, const char *cg
)
174 size_t target_len
, task_len
;
176 if (strcmp(cg
, "/") == 0 || strcmp(cg
, "./") == 0)
179 c2
= get_pid_cgroup(pid
, contrl
);
182 prune_init_slice(c2
);
185 target_len
= strlen(cg
);
186 task_len
= strlen(task_cg
);
188 /* Task is in the root cg, it can see everything. This case is
189 * not handled by the strmcps below, since they test for the
190 * last /, but that is the first / that we've chopped off
196 if (strcmp(cg
, task_cg
) == 0) {
200 if (target_len
< task_len
) {
201 /* looking up a parent dir */
202 if (strncmp(task_cg
, cg
, target_len
) == 0 && task_cg
[target_len
] == '/')
206 if (target_len
> task_len
) {
207 /* looking up a child dir */
208 if (strncmp(task_cg
, cg
, task_len
) == 0 && cg
[task_len
] == '/')
220 * querycg is /a/b/c/d/e
223 static char *get_next_cgroup_dir(const char *taskcg
, const char *querycg
)
227 if (strlen(taskcg
) <= strlen(querycg
)) {
228 lxcfs_error("%s\n", "I was fed bad input.");
232 if ((strcmp(querycg
, "/") == 0) || (strcmp(querycg
, "./") == 0))
233 start
= strdup(taskcg
+ 1);
235 start
= strdup(taskcg
+ strlen(querycg
) + 1);
238 end
= strchr(start
, '/');
245 * If pid is in /a/b/c/d, he may only act on things under cg=/a/b/c/d.
246 * If pid is in /a, he may act on /a/b, but not on /b.
247 * if the answer is false and nextcg is not NULL, then *nextcg will point
248 * to a string containing the next cgroup directory under cg, which must be
249 * freed by the caller.
251 static bool caller_is_in_ancestor(pid_t pid
, const char *contrl
, const char *cg
, char **nextcg
)
254 char *c2
= get_pid_cgroup(pid
, contrl
);
259 prune_init_slice(c2
);
262 * callers pass in '/' or './' (openat()) for root cgroup, otherwise
263 * they pass in a cgroup without leading '/'
265 * The original line here was:
266 * linecmp = *cg == '/' ? c2 : c2+1;
267 * TODO: I'm not sure why you'd want to increment when *cg != '/'?
268 * Serge, do you know?
270 if (*cg
== '/' || !strncmp(cg
, "./", 2))
274 if (strncmp(linecmp
, cg
, strlen(linecmp
)) != 0) {
276 *nextcg
= get_next_cgroup_dir(linecmp
, cg
);
287 static struct cgfs_files
*cgfs_get_key(const char *controller
,
288 const char *cgroup
, const char *file
)
294 struct cgfs_files
*newkey
;
296 cfd
= get_cgroup_fd(controller
);
300 if (file
&& *file
== '/')
303 if (file
&& strchr(file
, '/'))
306 /* Make sure we pass a relative path to *at() family of functions.
307 * . + /cgroup + / + file + \0
309 len
= strlen(cgroup
) + 3;
311 len
+= strlen(file
) + 1;
313 snprintf(fnam
, len
, "%s%s%s%s", dot_or_empty(cgroup
), cgroup
,
314 file
? "/" : "", file
? file
: "");
316 ret
= fstatat(cfd
, fnam
, &sb
, 0);
321 newkey
= malloc(sizeof(struct cgfs_files
));
324 newkey
->name
= must_copy_string(file
);
325 else if (strrchr(cgroup
, '/'))
326 newkey
->name
= must_copy_string(strrchr(cgroup
, '/'));
328 newkey
->name
= must_copy_string(cgroup
);
329 newkey
->uid
= sb
.st_uid
;
330 newkey
->gid
= sb
.st_gid
;
331 newkey
->mode
= sb
.st_mode
;
337 * Given a open file * to /proc/pid/{u,g}id_map, and an id
338 * valid in the caller's namespace, return the id mapped into
340 * Returns the mapped id, or -1 on error.
342 static unsigned int convert_id_to_ns(FILE *idfile
, unsigned int in_id
)
344 unsigned int nsuid
, // base id for a range in the idfile's namespace
345 hostuid
, // base id for a range in the caller's namespace
346 count
; // number of ids in this range
350 fseek(idfile
, 0L, SEEK_SET
);
351 while (fgets(line
, 400, idfile
)) {
352 ret
= sscanf(line
, "%u %u %u\n", &nsuid
, &hostuid
, &count
);
355 if (hostuid
+ count
< hostuid
|| nsuid
+ count
< nsuid
) {
357 * uids wrapped around - unexpected as this is a procfile,
360 lxcfs_error("pid wrapparound at entry %u %u %u in %s\n",
361 nsuid
, hostuid
, count
, line
);
364 if (hostuid
<= in_id
&& hostuid
+count
> in_id
) {
366 * now since hostuid <= in_id < hostuid+count, and
367 * hostuid+count and nsuid+count do not wrap around,
368 * we know that nsuid+(in_id-hostuid) which must be
369 * less that nsuid+(count) must not wrap around
371 return (in_id
- hostuid
) + nsuid
;
380 * for is_privileged_over,
381 * specify whether we require the calling uid to be root in his
384 #define NS_ROOT_REQD true
385 #define NS_ROOT_OPT false
389 static bool is_privileged_over(pid_t pid
, uid_t uid
, uid_t victim
, bool req_ns_root
)
396 if (victim
== -1 || uid
== -1)
400 * If the request is one not requiring root in the namespace,
401 * then having the same uid suffices. (i.e. uid 1000 has write
402 * access to files owned by uid 1000
404 if (!req_ns_root
&& uid
== victim
)
407 ret
= snprintf(fpath
, PROCLEN
, "/proc/%d/uid_map", pid
);
408 if (ret
< 0 || ret
>= PROCLEN
)
410 FILE *f
= fopen(fpath
, "re");
414 /* if caller's not root in his namespace, reject */
415 nsuid
= convert_id_to_ns(f
, uid
);
420 * If victim is not mapped into caller's ns, reject.
421 * XXX I'm not sure this check is needed given that fuse
422 * will be sending requests where the vfs has converted
424 nsuid
= convert_id_to_ns(f
, victim
);
435 static bool perms_include(int fmode
, mode_t req_mode
)
439 switch (req_mode
& O_ACCMODE
) {
447 r
= S_IROTH
| S_IWOTH
;
452 return ((fmode
& r
) == r
);
455 static void free_key(struct cgfs_files
*k
)
459 free_disarm(k
->name
);
464 * check whether a fuse context may access a cgroup dir or file
466 * If file is not null, it is a cgroup file to check under cg.
467 * If file is null, then we are checking perms on cg itself.
469 * For files we can check the mode of the list_keys result.
470 * For cgroups, we must make assumptions based on the files under the
471 * cgroup, because cgmanager doesn't tell us ownership/perms of cgroups
474 static bool fc_may_access(struct fuse_context
*fc
, const char *contrl
, const char *cg
, const char *file
, mode_t mode
)
476 struct cgfs_files
*k
= NULL
;
479 k
= cgfs_get_key(contrl
, cg
, file
);
483 if (is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
484 if (perms_include(k
->mode
>> 6, mode
)) {
489 if (fc
->gid
== k
->gid
) {
490 if (perms_include(k
->mode
>> 3, mode
)) {
495 ret
= perms_include(k
->mode
, mode
);
502 int cg_getattr(const char *path
, struct stat
*sb
)
505 struct fuse_context
*fc
= fuse_get_context();
507 char *last
= NULL
, *path1
, *path2
;
508 struct cgfs_files
*k
= NULL
;
510 const char *controller
= NULL
;
514 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
517 memset(sb
, 0, sizeof(struct stat
));
519 if (clock_gettime(CLOCK_REALTIME
, &now
) < 0)
522 sb
->st_uid
= sb
->st_gid
= 0;
523 sb
->st_atim
= sb
->st_mtim
= sb
->st_ctim
= now
;
526 if (strcmp(path
, "/cgroup") == 0) {
527 sb
->st_mode
= S_IFDIR
| 00755;
532 controller
= pick_controller_from_path(fc
, path
);
535 cgroup
= find_cgroup_in_path(path
);
537 /* this is just /cgroup/controller, return it as a dir */
538 sb
->st_mode
= S_IFDIR
| 00755;
543 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
553 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
554 if (initpid
<= 1 || is_shared_pidns(initpid
))
556 /* check that cgcopy is either a child cgroup of cgdir, or listed in its keys.
557 * Then check that caller's cgroup is under path if last is a child
558 * cgroup, or cgdir if last is a file */
560 if (is_child_cgroup(controller
, path1
, path2
)) {
561 if (!caller_may_see_dir(initpid
, controller
, cgroup
)) {
565 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
566 /* this is just /cgroup/controller, return it as a dir */
567 sb
->st_mode
= S_IFDIR
| 00555;
572 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
)) {
577 // get uid, gid, from '/tasks' file and make up a mode
578 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
579 sb
->st_mode
= S_IFDIR
| 00755;
580 k
= cgfs_get_key(controller
, cgroup
, NULL
);
582 sb
->st_uid
= sb
->st_gid
= 0;
593 if ((k
= cgfs_get_key(controller
, path1
, path2
)) != NULL
) {
594 sb
->st_mode
= S_IFREG
| k
->mode
;
600 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
613 * Chown all the files in the cgroup directory. We do this when we create a
614 * cgroup on behalf of a user.
616 static void chown_all_cgroup_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
618 struct dirent
*direntp
;
619 char path
[MAXPATHLEN
];
624 len
= strlen(dirname
);
625 if (len
>= MAXPATHLEN
) {
626 lxcfs_error("Pathname too long: %s\n", dirname
);
630 fd1
= openat(fd
, dirname
, O_DIRECTORY
);
636 lxcfs_error("Failed to open %s\n", dirname
);
640 while ((direntp
= readdir(d
))) {
641 if (!strcmp(direntp
->d_name
, ".") || !strcmp(direntp
->d_name
, ".."))
643 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
644 if (ret
< 0 || ret
>= MAXPATHLEN
) {
645 lxcfs_error("Pathname too long under %s\n", dirname
);
648 if (fchownat(fd
, path
, uid
, gid
, 0) < 0)
649 lxcfs_error("Failed to chown file %s to %u:%u", path
, uid
, gid
);
654 static int cgfs_create(const char *controller
, const char *cg
, uid_t uid
, gid_t gid
)
660 cfd
= get_cgroup_fd(controller
);
664 /* Make sure we pass a relative path to *at() family of functions.
667 len
= strlen(cg
) + 2;
668 dirnam
= alloca(len
);
669 snprintf(dirnam
, len
, "%s%s", dot_or_empty(cg
), cg
);
671 if (mkdirat(cfd
, dirnam
, 0755) < 0)
674 if (uid
== 0 && gid
== 0)
677 if (fchownat(cfd
, dirnam
, uid
, gid
, 0) < 0)
680 chown_all_cgroup_files(dirnam
, uid
, gid
, cfd
);
685 int cg_mkdir(const char *path
, mode_t mode
)
687 struct fuse_context
*fc
= fuse_get_context();
688 char *last
= NULL
, *path1
, *cgdir
= NULL
, *controller
, *next
= NULL
;
692 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
695 controller
= pick_controller_from_path(fc
, path
);
697 return errno
== ENOENT
? -EPERM
: -errno
;
699 cgroup
= find_cgroup_in_path(path
);
703 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
709 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
710 if (initpid
<= 1 || is_shared_pidns(initpid
))
712 if (!caller_is_in_ancestor(initpid
, controller
, path1
, &next
)) {
715 else if (last
&& strcmp(next
, last
) == 0)
722 if (!fc_may_access(fc
, controller
, path1
, NULL
, O_RDWR
)) {
726 if (!caller_is_in_ancestor(initpid
, controller
, path1
, NULL
)) {
731 ret
= cgfs_create(controller
, cgroup
, fc
->uid
, fc
->gid
);
739 static bool recursive_rmdir(const char *dirname
, int fd
, const int cfd
)
741 struct dirent
*direntp
;
744 char pathname
[MAXPATHLEN
];
747 dupfd
= dup(fd
); // fdopendir() does bad things once it uses an fd.
751 dir
= fdopendir(dupfd
);
753 lxcfs_debug("Failed to open %s: %s.\n", dirname
, strerror(errno
));
758 while ((direntp
= readdir(dir
))) {
762 if (!strcmp(direntp
->d_name
, ".") ||
763 !strcmp(direntp
->d_name
, ".."))
766 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
767 if (rc
< 0 || rc
>= MAXPATHLEN
) {
768 lxcfs_error("%s\n", "Pathname too long.");
772 rc
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
774 lxcfs_debug("Failed to stat %s: %s.\n", pathname
, strerror(errno
));
777 if (S_ISDIR(mystat
.st_mode
))
778 if (!recursive_rmdir(pathname
, fd
, cfd
))
779 lxcfs_debug("Error removing %s.\n", pathname
);
783 if (closedir(dir
) < 0) {
784 lxcfs_error("Failed to close directory %s: %s\n", dirname
, strerror(errno
));
788 if (unlinkat(cfd
, dirname
, AT_REMOVEDIR
) < 0) {
789 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
798 static bool cgfs_remove(const char *controller
, const char *cg
)
805 cfd
= get_cgroup_fd(controller
);
809 /* Make sure we pass a relative path to *at() family of functions.
812 len
= strlen(cg
) + 2;
813 dirnam
= alloca(len
);
814 snprintf(dirnam
, len
, "%s%s", dot_or_empty(cg
), cg
);
816 fd
= openat(cfd
, dirnam
, O_DIRECTORY
);
820 bret
= recursive_rmdir(dirnam
, fd
, cfd
);
825 int cg_rmdir(const char *path
)
827 struct fuse_context
*fc
= fuse_get_context();
828 char *last
= NULL
, *cgdir
= NULL
, *controller
, *next
= NULL
;
832 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
835 controller
= pick_controller_from_path(fc
, path
);
836 if (!controller
) /* Someone's trying to delete "/cgroup". */
839 cgroup
= find_cgroup_in_path(path
);
840 if (!cgroup
) /* Someone's trying to delete a controller e.g. "/blkio". */
843 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
845 /* Someone's trying to delete a cgroup on the same level as the
846 * "/lxc" cgroup e.g. rmdir "/cgroup/blkio/lxc" or
847 * rmdir "/cgroup/blkio/init.slice".
853 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
854 if (initpid
<= 1 || is_shared_pidns(initpid
))
856 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, &next
)) {
857 if (!last
|| (next
&& (strcmp(next
, last
) == 0)))
864 if (!fc_may_access(fc
, controller
, cgdir
, NULL
, O_WRONLY
)) {
868 if (!caller_is_in_ancestor(initpid
, controller
, cgroup
, NULL
)) {
873 if (!cgfs_remove(controller
, cgroup
)) {
886 static bool cgfs_chmod_file(const char *controller
, const char *file
, mode_t mode
)
892 cfd
= get_cgroup_fd(controller
);
896 /* Make sure we pass a relative path to *at() family of functions.
899 len
= strlen(file
) + 2;
900 pathname
= alloca(len
);
901 snprintf(pathname
, len
, "%s%s", dot_or_empty(file
), file
);
902 if (fchmodat(cfd
, pathname
, mode
, 0) < 0)
907 int cg_chmod(const char *path
, mode_t mode
)
909 struct fuse_context
*fc
= fuse_get_context();
910 char * cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
911 struct cgfs_files
*k
= NULL
;
915 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
918 if (strcmp(path
, "/cgroup") == 0)
921 controller
= pick_controller_from_path(fc
, path
);
923 return errno
== ENOENT
? -EPERM
: -errno
;
925 cgroup
= find_cgroup_in_path(path
);
927 /* this is just /cgroup/controller */
930 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
940 if (is_child_cgroup(controller
, path1
, path2
)) {
941 // get uid, gid, from '/tasks' file and make up a mode
942 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
943 k
= cgfs_get_key(controller
, cgroup
, "tasks");
946 k
= cgfs_get_key(controller
, path1
, path2
);
954 * This being a fuse request, the uid and gid must be valid
955 * in the caller's namespace. So we can just check to make
956 * sure that the caller is root in his uid, and privileged
957 * over the file's current owner.
959 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_OPT
)) {
964 if (!cgfs_chmod_file(controller
, cgroup
, mode
)) {
976 static int is_dir(const char *path
, int fd
)
979 int ret
= fstatat(fd
, path
, &statbuf
, fd
);
980 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
985 static int chown_tasks_files(const char *dirname
, uid_t uid
, gid_t gid
, int fd
)
990 len
= strlen(dirname
) + strlen("/cgroup.procs") + 1;
992 snprintf(fname
, len
, "%s/tasks", dirname
);
993 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
995 snprintf(fname
, len
, "%s/cgroup.procs", dirname
);
996 if (fchownat(fd
, fname
, uid
, gid
, 0) != 0)
1001 static int cgfs_chown_file(const char *controller
, const char *file
, uid_t uid
,
1008 cfd
= get_cgroup_fd(controller
);
1012 /* Make sure we pass a relative path to *at() family of functions.
1015 len
= strlen(file
) + 2;
1016 pathname
= alloca(len
);
1017 snprintf(pathname
, len
, "%s%s", dot_or_empty(file
), file
);
1018 if (fchownat(cfd
, pathname
, uid
, gid
, 0) < 0)
1021 if (is_dir(pathname
, cfd
))
1022 return chown_tasks_files(pathname
, uid
, gid
, cfd
);
1027 int cg_chown(const char *path
, uid_t uid
, gid_t gid
)
1029 struct fuse_context
*fc
= fuse_get_context();
1030 char *cgdir
= NULL
, *last
= NULL
, *path1
, *path2
, *controller
;
1031 struct cgfs_files
*k
= NULL
;
1035 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1038 if (strcmp(path
, "/cgroup") == 0)
1041 controller
= pick_controller_from_path(fc
, path
);
1043 return errno
== ENOENT
? -EPERM
: -errno
;
1045 cgroup
= find_cgroup_in_path(path
);
1047 /* this is just /cgroup/controller */
1050 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1060 if (is_child_cgroup(controller
, path1
, path2
)) {
1061 // get uid, gid, from '/tasks' file and make up a mode
1062 // That is a hack, until cgmanager gains a GetCgroupPerms fn.
1063 k
= cgfs_get_key(controller
, cgroup
, "tasks");
1066 k
= cgfs_get_key(controller
, path1
, path2
);
1074 * This being a fuse request, the uid and gid must be valid
1075 * in the caller's namespace. So we can just check to make
1076 * sure that the caller is root in his uid, and privileged
1077 * over the file's current owner.
1079 if (!is_privileged_over(fc
->pid
, fc
->uid
, k
->uid
, NS_ROOT_REQD
)) {
1084 ret
= cgfs_chown_file(controller
, cgroup
, uid
, gid
);
1093 int cg_open(const char *path
, struct fuse_file_info
*fi
)
1096 char *last
= NULL
, *path1
, *path2
, * cgdir
= NULL
, *controller
;
1097 struct cgfs_files
*k
= NULL
;
1098 struct file_info
*file_info
;
1099 struct fuse_context
*fc
= fuse_get_context();
1102 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1105 controller
= pick_controller_from_path(fc
, path
);
1108 cgroup
= find_cgroup_in_path(path
);
1112 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
1121 k
= cgfs_get_key(controller
, path1
, path2
);
1128 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1129 if (initpid
<= 1 || is_shared_pidns(initpid
))
1131 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
1135 if (!fc_may_access(fc
, controller
, path1
, path2
, fi
->flags
)) {
1140 /* we'll free this at cg_release */
1141 file_info
= malloc(sizeof(*file_info
));
1146 file_info
->controller
= must_copy_string(controller
);
1147 file_info
->cgroup
= must_copy_string(path1
);
1148 file_info
->file
= must_copy_string(path2
);
1149 file_info
->type
= LXC_TYPE_CGFILE
;
1150 file_info
->buf
= NULL
;
1151 file_info
->buflen
= 0;
1153 fi
->fh
= PTR_TO_UINT64(file_info
);
1161 #define POLLIN_SET ( EPOLLIN | EPOLLHUP | EPOLLRDHUP )
1164 * pid_to_ns - reads pids from a ucred over a socket, then writes the
1165 * int value back over the socket. This shifts the pid from the
1166 * sender's pidns into tpid's pidns.
1168 static int pid_to_ns(int sock
, pid_t tpid
)
1173 while (recv_creds(sock
, &cred
, &v
)) {
1177 if (write(sock
, &cred
.pid
, sizeof(pid_t
)) != sizeof(pid_t
))
1185 * pid_ns_clone_wrapper - wraps pid_to_ns or pid_from_ns for usage
1186 * with clone(). This simply writes '1' as ACK back to the parent
1187 * before calling the actual wrapped function.
1189 static int pid_ns_clone_wrapper(void *arg
) {
1190 struct pid_ns_clone_args
* args
= (struct pid_ns_clone_args
*) arg
;
1193 close(args
->cpipe
[0]);
1194 if (write(args
->cpipe
[1], &b
, sizeof(char)) < 0)
1195 lxcfs_error("(child): error on write: %s.\n", strerror(errno
));
1196 close(args
->cpipe
[1]);
1197 return args
->wrapped(args
->sock
, args
->tpid
);
1201 * pid_to_ns_wrapper: when you setns into a pidns, you yourself remain
1202 * in your old pidns. Only children which you clone will be in the target
1203 * pidns. So the pid_to_ns_wrapper does the setns, then clones a child to
1204 * actually convert pids.
1206 * Note: glibc's fork() does not respect pidns, which can lead to failed
1207 * assertions inside glibc (and thus failed forks) if the child's pid in
1208 * the pidns and the parent pid outside are identical. Using clone prevents
1211 static void pid_to_ns_wrapper(int sock
, pid_t tpid
)
1213 int newnsfd
= -1, ret
, cpipe
[2];
1218 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1219 if (ret
< 0 || ret
>= sizeof(fnam
))
1221 newnsfd
= open(fnam
, O_RDONLY
);
1224 if (setns(newnsfd
, 0) < 0)
1228 if (pipe(cpipe
) < 0)
1231 struct pid_ns_clone_args args
= {
1235 .wrapped
= &pid_to_ns
1237 size_t stack_size
= sysconf(_SC_PAGESIZE
);
1238 void *stack
= alloca(stack_size
);
1240 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
1244 /* Give the child 1 second to be done forking and write its ack. */
1245 if (!wait_for_sock(cpipe
[0], 1))
1247 ret
= read(cpipe
[0], &v
, 1);
1248 if (ret
!= sizeof(char) || v
!= '1')
1251 if (!wait_for_pid(cpid
))
1257 * append pid to *src.
1258 * src: a pointer to a char* in which ot append the pid.
1259 * sz: the number of characters printed so far, minus trailing \0.
1260 * asz: the allocated size so far
1261 * pid: the pid to append
1263 static void must_strcat_pid(char **src
, size_t *sz
, size_t *asz
, pid_t pid
)
1265 must_strcat(src
, sz
, asz
, "%d\n", (int)pid
);
1269 * To read cgroup files with a particular pid, we will setns into the child
1270 * pidns, open a pipe, fork a child - which will be the first to really be in
1271 * the child ns - which does the cgfs_get_value and writes the data to the pipe.
1273 static bool do_read_pids(pid_t tpid
, const char *contrl
, const char *cg
,
1274 const char *file
, char **d
)
1276 int sock
[2] = {-1, -1};
1277 char *tmpdata
= NULL
;
1279 pid_t qpid
, cpid
= -1;
1280 bool answer
= false;
1283 size_t sz
= 0, asz
= 0;
1285 if (!cgroup_ops
->get(cgroup_ops
, contrl
, cg
, file
, &tmpdata
))
1289 * Now we read the pids from returned data one by one, pass
1290 * them into a child in the target namespace, read back the
1291 * translated pids, and put them into our to-return data
1294 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1295 perror("socketpair");
1304 if (!cpid
) // child - exits when done
1305 pid_to_ns_wrapper(sock
[1], tpid
);
1307 char *ptr
= tmpdata
;
1310 while (sscanf(ptr
, "%d\n", &qpid
) == 1) {
1312 ret
= send_creds(sock
[0], &cred
, v
, true);
1314 if (ret
== SEND_CREDS_NOTSK
)
1316 if (ret
== SEND_CREDS_FAIL
)
1319 // read converted results
1320 if (!wait_for_sock(sock
[0], 2)) {
1321 lxcfs_error("Timed out waiting for pid from child: %s.\n", strerror(errno
));
1324 if (read(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1325 lxcfs_error("Error reading pid from child: %s.\n", strerror(errno
));
1328 must_strcat_pid(d
, &sz
, &asz
, qpid
);
1330 ptr
= strchr(ptr
, '\n');
1336 cred
.pid
= getpid();
1338 if (send_creds(sock
[0], &cred
, v
, true) != SEND_CREDS_OK
) {
1339 // failed to ask child to exit
1340 lxcfs_error("Failed to ask child to exit: %s.\n", strerror(errno
));
1350 if (sock
[0] != -1) {
1357 int cg_read(const char *path
, char *buf
, size_t size
, off_t offset
,
1358 struct fuse_file_info
*fi
)
1360 struct fuse_context
*fc
= fuse_get_context();
1361 struct file_info
*f
= INTTYPE_TO_PTR(fi
->fh
);
1362 struct cgfs_files
*k
= NULL
;
1367 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1370 if (f
->type
!= LXC_TYPE_CGFILE
) {
1371 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_read.");
1381 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1387 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_RDONLY
)) {
1392 if (strcmp(f
->file
, "tasks") == 0 ||
1393 strcmp(f
->file
, "/tasks") == 0 ||
1394 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1395 strcmp(f
->file
, "cgroup.procs") == 0)
1396 // special case - we have to translate the pids
1397 r
= do_read_pids(fc
->pid
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1399 r
= cgroup_ops
->get(cgroup_ops
, f
->controller
, f
->cgroup
, f
->file
, &data
);
1413 memcpy(buf
, data
, s
);
1414 if (s
> 0 && s
< size
&& data
[s
-1] != '\n')
1424 int cg_opendir(const char *path
, struct fuse_file_info
*fi
)
1426 struct fuse_context
*fc
= fuse_get_context();
1428 struct file_info
*dir_info
;
1429 char *controller
= NULL
;
1431 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1434 if (strcmp(path
, "/cgroup") == 0) {
1438 // return list of keys for the controller, and list of child cgroups
1439 controller
= pick_controller_from_path(fc
, path
);
1443 cgroup
= find_cgroup_in_path(path
);
1445 /* this is just /cgroup/controller, return its contents */
1450 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
1451 if (initpid
<= 1 || is_shared_pidns(initpid
))
1454 if (!caller_may_see_dir(initpid
, controller
, cgroup
))
1456 if (!fc_may_access(fc
, controller
, cgroup
, NULL
, O_RDONLY
))
1460 /* we'll free this at cg_releasedir */
1461 dir_info
= malloc(sizeof(*dir_info
));
1464 dir_info
->controller
= must_copy_string(controller
);
1465 dir_info
->cgroup
= must_copy_string(cgroup
);
1466 dir_info
->type
= LXC_TYPE_CGDIR
;
1467 dir_info
->buf
= NULL
;
1468 dir_info
->file
= NULL
;
1469 dir_info
->buflen
= 0;
1471 fi
->fh
= PTR_TO_UINT64(dir_info
);
1475 int cg_release(const char *path
, struct fuse_file_info
*fi
)
1477 do_release_file_info(fi
);
1481 int cg_releasedir(const char *path
, struct fuse_file_info
*fi
)
1483 do_release_file_info(fi
);
1487 static FILE *open_pids_file(const char *controller
, const char *cgroup
)
1493 cfd
= get_cgroup_fd(controller
);
1497 /* Make sure we pass a relative path to *at() family of functions.
1498 * . + /cgroup + / "cgroup.procs" + \0
1500 len
= strlen(cgroup
) + strlen("cgroup.procs") + 3;
1501 pathname
= alloca(len
);
1502 snprintf(pathname
, len
, "%s%s/cgroup.procs", dot_or_empty(cgroup
), cgroup
);
1504 fd
= openat(cfd
, pathname
, O_WRONLY
);
1508 return fdopen(fd
, "w");
1511 static int pid_from_ns(int sock
, pid_t tpid
)
1521 if (!wait_for_sock(sock
, 2)) {
1522 lxcfs_error("%s\n", "Timeout reading from parent.");
1525 if ((ret
= read(sock
, &vpid
, sizeof(pid_t
))) != sizeof(pid_t
)) {
1526 lxcfs_error("Bad read from parent: %s.\n", strerror(errno
));
1529 if (vpid
== -1) // done
1533 if (send_creds(sock
, &cred
, v
, true) != SEND_CREDS_OK
) {
1535 cred
.pid
= getpid();
1536 if (send_creds(sock
, &cred
, v
, false) != SEND_CREDS_OK
)
1543 static void pid_from_ns_wrapper(int sock
, pid_t tpid
)
1545 int newnsfd
= -1, ret
, cpipe
[2];
1550 ret
= snprintf(fnam
, sizeof(fnam
), "/proc/%d/ns/pid", tpid
);
1551 if (ret
< 0 || ret
>= sizeof(fnam
))
1553 newnsfd
= open(fnam
, O_RDONLY
);
1556 if (setns(newnsfd
, 0) < 0)
1560 if (pipe(cpipe
) < 0)
1563 struct pid_ns_clone_args args
= {
1567 .wrapped
= &pid_from_ns
1569 size_t stack_size
= sysconf(_SC_PAGESIZE
);
1570 void *stack
= alloca(stack_size
);
1572 cpid
= clone(pid_ns_clone_wrapper
, stack
+ stack_size
, SIGCHLD
, &args
);
1576 // give the child 1 second to be done forking and
1578 if (!wait_for_sock(cpipe
[0], 1))
1580 ret
= read(cpipe
[0], &v
, 1);
1581 if (ret
!= sizeof(char) || v
!= '1')
1584 if (!wait_for_pid(cpid
))
1590 * get_pid_creds: get the real uid and gid of @pid from
1592 * (XXX should we use euid here?)
1594 static void get_pid_creds(pid_t pid
, uid_t
*uid
, gid_t
*gid
)
1603 sprintf(line
, "/proc/%d/status", pid
);
1604 if ((f
= fopen(line
, "re")) == NULL
) {
1605 lxcfs_error("Error opening %s: %s\n", line
, strerror(errno
));
1608 while (fgets(line
, 400, f
)) {
1609 if (strncmp(line
, "Uid:", 4) == 0) {
1610 if (sscanf(line
+4, "%u", &u
) != 1) {
1611 lxcfs_error("bad uid line for pid %u\n", pid
);
1616 } else if (strncmp(line
, "Gid:", 4) == 0) {
1617 if (sscanf(line
+4, "%u", &g
) != 1) {
1618 lxcfs_error("bad gid line for pid %u\n", pid
);
1629 * Given host @uid, return the uid to which it maps in
1630 * @pid's user namespace, or -1 if none.
1632 static bool hostuid_to_ns(uid_t uid
, pid_t pid
, uid_t
*answer
)
1637 sprintf(line
, "/proc/%d/uid_map", pid
);
1638 if ((f
= fopen(line
, "re")) == NULL
) {
1642 *answer
= convert_id_to_ns(f
, uid
);
1651 * May the requestor @r move victim @v to a new cgroup?
1652 * This is allowed if
1653 * . they are the same task
1654 * . they are ownedy by the same uid
1655 * . @r is root on the host, or
1656 * . @v's uid is mapped into @r's where @r is root.
1658 static bool may_move_pid(pid_t r
, uid_t r_uid
, pid_t v
)
1660 uid_t v_uid
, tmpuid
;
1667 get_pid_creds(v
, &v_uid
, &v_gid
);
1670 if (hostuid_to_ns(r_uid
, r
, &tmpuid
) && tmpuid
== 0
1671 && hostuid_to_ns(v_uid
, r
, &tmpuid
))
1676 static bool do_write_pids(pid_t tpid
, uid_t tuid
, const char *contrl
,
1677 const char *cg
, const char *file
, const char *buf
)
1679 int sock
[2] = {-1, -1};
1680 pid_t qpid
, cpid
= -1;
1681 FILE *pids_file
= NULL
;
1682 bool answer
= false, fail
= false;
1684 pids_file
= open_pids_file(contrl
, cg
);
1689 * write the pids to a socket, have helper in writer's pidns
1690 * call movepid for us
1692 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, sock
) < 0) {
1693 perror("socketpair");
1701 if (!cpid
) { // child
1703 pid_from_ns_wrapper(sock
[1], tpid
);
1706 const char *ptr
= buf
;
1707 while (sscanf(ptr
, "%d", &qpid
) == 1) {
1711 if (write(sock
[0], &qpid
, sizeof(qpid
)) != sizeof(qpid
)) {
1712 lxcfs_error("Error writing pid to child: %s.\n", strerror(errno
));
1716 if (recv_creds(sock
[0], &cred
, &v
)) {
1718 if (!may_move_pid(tpid
, tuid
, cred
.pid
)) {
1722 if (fprintf(pids_file
, "%d", (int) cred
.pid
) < 0)
1727 ptr
= strchr(ptr
, '\n');
1733 /* All good, write the value */
1735 if (write(sock
[0], &qpid
,sizeof(qpid
)) != sizeof(qpid
))
1736 lxcfs_error("%s\n", "Warning: failed to ask child to exit.");
1744 if (sock
[0] != -1) {
1749 if (fclose(pids_file
) != 0)
1755 static bool write_string(const char *fnam
, const char *string
, int fd
)
1760 f
= fdopen(fd
, "w");
1764 len
= strlen(string
);
1765 ret
= fwrite(string
, 1, len
, f
);
1767 lxcfs_error("%s - Error writing \"%s\" to \"%s\"\n",
1768 strerror(errno
), string
, fnam
);
1773 if (fclose(f
) < 0) {
1774 lxcfs_error("%s - Failed to close \"%s\"\n", strerror(errno
), fnam
);
1781 static bool cgfs_set_value(const char *controller
, const char *cgroup
,
1782 const char *file
, const char *value
)
1788 cfd
= get_cgroup_fd(controller
);
1792 /* Make sure we pass a relative path to *at() family of functions.
1793 * . + /cgroup + / + file + \0
1795 len
= strlen(cgroup
) + strlen(file
) + 3;
1797 ret
= snprintf(fnam
, len
, "%s%s/%s", dot_or_empty(cgroup
), cgroup
, file
);
1798 if (ret
< 0 || (size_t)ret
>= len
)
1801 fd
= openat(cfd
, fnam
, O_WRONLY
);
1805 return write_string(fnam
, value
, fd
);
1808 int cg_write(const char *path
, const char *buf
, size_t size
, off_t offset
,
1809 struct fuse_file_info
*fi
)
1811 struct fuse_context
*fc
= fuse_get_context();
1812 char *localbuf
= NULL
;
1813 struct cgfs_files
*k
= NULL
;
1814 struct file_info
*f
= INTTYPE_TO_PTR(fi
->fh
);
1817 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1820 if (f
->type
!= LXC_TYPE_CGFILE
) {
1821 lxcfs_error("%s\n", "Internal error: directory cache info used in cg_write.");
1828 localbuf
= alloca(size
+1);
1829 localbuf
[size
] = '\0';
1830 memcpy(localbuf
, buf
, size
);
1832 if ((k
= cgfs_get_key(f
->controller
, f
->cgroup
, f
->file
)) == NULL
) {
1837 if (!fc_may_access(fc
, f
->controller
, f
->cgroup
, f
->file
, O_WRONLY
)) {
1842 if (strcmp(f
->file
, "tasks") == 0 ||
1843 strcmp(f
->file
, "/tasks") == 0 ||
1844 strcmp(f
->file
, "/cgroup.procs") == 0 ||
1845 strcmp(f
->file
, "cgroup.procs") == 0)
1846 // special case - we have to translate the pids
1847 r
= do_write_pids(fc
->pid
, fc
->uid
, f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1849 r
= cgfs_set_value(f
->controller
, f
->cgroup
, f
->file
, localbuf
);
1859 static bool cgfs_iterate_cgroup(const char *controller
, const char *cgroup
,
1860 bool directories
, void ***list
, size_t typesize
,
1861 void *(*iterator
)(const char *, const char *, const char *))
1866 char pathname
[MAXPATHLEN
];
1867 size_t sz
= 0, asz
= 0;
1868 struct dirent
*dirent
;
1871 cfd
= get_cgroup_fd(controller
);
1876 /* Make sure we pass a relative path to *at() family of functions. */
1877 len
= strlen(cgroup
) + 1 /* . */ + 1 /* \0 */;
1879 ret
= snprintf(cg
, len
, "%s%s", dot_or_empty(cgroup
), cgroup
);
1880 if (ret
< 0 || (size_t)ret
>= len
) {
1881 lxcfs_error("Pathname too long under %s\n", cgroup
);
1885 fd
= openat(cfd
, cg
, O_DIRECTORY
);
1889 dir
= fdopendir(fd
);
1893 while ((dirent
= readdir(dir
))) {
1896 if (!strcmp(dirent
->d_name
, ".") ||
1897 !strcmp(dirent
->d_name
, ".."))
1900 ret
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", cg
, dirent
->d_name
);
1901 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1902 lxcfs_error("Pathname too long under %s\n", cg
);
1906 ret
= fstatat(cfd
, pathname
, &mystat
, AT_SYMLINK_NOFOLLOW
);
1908 lxcfs_error("Failed to stat %s: %s\n", pathname
, strerror(errno
));
1911 if ((!directories
&& !S_ISREG(mystat
.st_mode
)) ||
1912 (directories
&& !S_ISDIR(mystat
.st_mode
)))
1919 tmp
= realloc(*list
, asz
* typesize
);
1923 (*list
)[sz
] = (*iterator
)(controller
, cg
, dirent
->d_name
);
1924 (*list
)[sz
+1] = NULL
;
1927 if (closedir(dir
) < 0) {
1928 lxcfs_error("Failed closedir for %s: %s\n", cgroup
, strerror(errno
));
1934 static void *make_key_list_entry(const char *controller
, const char *cgroup
,
1935 const char *dir_entry
)
1937 struct cgfs_files
*entry
;
1939 entry
= cgfs_get_key(controller
, cgroup
, dir_entry
);
1941 lxcfs_error("Failed to retrieve files under %s:%s\n",
1942 controller
, cgroup
);
1946 static bool cgfs_list_keys(const char *controller
, const char *cgroup
,
1947 struct cgfs_files
***keys
)
1949 return cgfs_iterate_cgroup(controller
, cgroup
, false, (void ***)keys
,
1950 sizeof(*keys
), &make_key_list_entry
);
1953 static void *make_children_list_entry(const char *controller
,
1954 const char *cgroup
, const char *dir_entry
)
1956 return strdup(dir_entry
);
1959 static bool cgfs_list_children(const char *controller
, const char *cgroup
,
1962 return cgfs_iterate_cgroup(controller
, cgroup
, true, (void ***)list
,
1963 sizeof(*list
), &make_children_list_entry
);
1966 static void free_keys(struct cgfs_files
**keys
)
1971 for (int i
= 0; keys
[i
]; i
++)
1977 int cg_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
1978 off_t offset
, struct fuse_file_info
*fi
)
1980 struct file_info
*d
= INTTYPE_TO_PTR(fi
->fh
);
1981 struct cgfs_files
**list
= NULL
;
1983 char *nextcg
= NULL
;
1984 struct fuse_context
*fc
= fuse_get_context();
1985 char **clist
= NULL
;
1987 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
1990 if (filler(buf
, ".", NULL
, 0) != 0 || filler(buf
, "..", NULL
, 0) != 0)
1993 if (d
->type
!= LXC_TYPE_CGDIR
) {
1994 lxcfs_error("%s\n", "Internal error: file cache info used in readdir.");
1997 if (!d
->cgroup
&& !d
->controller
) {
1999 * ls /var/lib/lxcfs/cgroup - just show list of controllers.
2000 * This only works with the legacy hierarchy.
2002 for (struct hierarchy
**h
= cgroup_ops
->hierarchies
; h
&& *h
; h
++) {
2003 if (is_unified_hierarchy(*h
))
2006 if ((*h
)->__controllers
&& filler(buf
, (*h
)->__controllers
, NULL
, 0))
2013 if (!cgfs_list_keys(d
->controller
, d
->cgroup
, &list
)) {
2014 // not a valid cgroup
2019 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2020 if (initpid
<= 1 || is_shared_pidns(initpid
))
2022 if (!caller_is_in_ancestor(initpid
, d
->controller
, d
->cgroup
, &nextcg
)) {
2024 ret
= filler(buf
, nextcg
, NULL
, 0);
2035 for (i
= 0; list
&& list
[i
]; i
++) {
2036 if (filler(buf
, list
[i
]->name
, NULL
, 0) != 0) {
2042 // now get the list of child cgroups
2044 if (!cgfs_list_children(d
->controller
, d
->cgroup
, &clist
)) {
2049 for (i
= 0; clist
[i
]; i
++) {
2050 if (filler(buf
, clist
[i
], NULL
, 0) != 0) {
2061 for (i
= 0; clist
[i
]; i
++)
2068 int cg_access(const char *path
, int mode
)
2072 char *path1
, *path2
, *controller
;
2073 char *last
= NULL
, *cgdir
= NULL
;
2074 struct cgfs_files
*k
= NULL
;
2075 struct fuse_context
*fc
= fuse_get_context();
2077 if (!fc
|| !cgroup_ops
|| pure_unified_layout(cgroup_ops
))
2080 if (strcmp(path
, "/cgroup") == 0)
2083 controller
= pick_controller_from_path(fc
, path
);
2086 cgroup
= find_cgroup_in_path(path
);
2088 // access("/sys/fs/cgroup/systemd", mode) - rx allowed, w not
2089 if ((mode
& W_OK
) == 0)
2094 get_cgdir_and_path(cgroup
, &cgdir
, &last
);
2103 k
= cgfs_get_key(controller
, path1
, path2
);
2105 if ((mode
& W_OK
) == 0)
2113 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
2114 if (initpid
<= 1 || is_shared_pidns(initpid
))
2116 if (!caller_may_see_dir(initpid
, controller
, path1
)) {
2120 if (!fc_may_access(fc
, controller
, path1
, path2
, mode
)) {