]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dev-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
11 #include "nulstr-util.h"
12 #include "path-util.h"
13 #include "umask-util.h"
14 #include "user-util.h"
16 int dev_setup(const char *prefix
, uid_t uid
, gid_t gid
) {
17 static const char symlinks
[] =
18 "-/proc/kcore\0" "/dev/core\0"
19 "/proc/self/fd\0" "/dev/fd\0"
20 "/proc/self/fd/0\0" "/dev/stdin\0"
21 "/proc/self/fd/1\0" "/dev/stdout\0"
22 "/proc/self/fd/2\0" "/dev/stderr\0";
27 NULSTR_FOREACH_PAIR(j
, k
, symlinks
) {
28 _cleanup_free_
char *link_name
= NULL
;
34 if (access(j
, F_OK
) < 0)
39 link_name
= path_join(prefix
, k
);
47 r
= symlink_label(j
, n
);
49 log_debug_errno(r
, "Failed to symlink %s to %s: %m", j
, n
);
51 if (uid
!= UID_INVALID
|| gid
!= GID_INVALID
)
52 if (lchown(n
, uid
, gid
) < 0)
53 log_debug_errno(errno
, "Failed to chown %s: %m", n
);
59 int make_inaccessible_nodes(
60 const char *runtime_dir
,
68 { "/systemd", S_IFDIR
| 0755 },
69 { "/systemd/inaccessible", S_IFDIR
| 0000 },
70 { "/systemd/inaccessible/reg", S_IFREG
| 0000 },
71 { "/systemd/inaccessible/dir", S_IFDIR
| 0000 },
72 { "/systemd/inaccessible/fifo", S_IFIFO
| 0000 },
73 { "/systemd/inaccessible/sock", S_IFSOCK
| 0000 },
75 /* The following two are likely to fail if we lack the privs for it (for example in an userns
76 * environment, if CAP_SYS_MKNOD is missing, or if a device node policy prohibit major/minor of 0
77 * device nodes to be created). But that's entirely fine. Consumers of these files should carry
78 * fallback to use a different node then, for example <root>/inaccessible/sock, which is close
79 * enough in behaviour and semantics for most uses. */
80 { "/systemd/inaccessible/chr", S_IFCHR
| 0000 },
81 { "/systemd/inaccessible/blk", S_IFBLK
| 0000 },
84 _cleanup_umask_ mode_t u
;
93 /* Set up inaccessible (and empty) file nodes of all types. This are used to as mount sources for over-mounting
94 * ("masking") file nodes that shall become inaccessible and empty for specific containers or services. We try
95 * to lock down these nodes as much as we can, but otherwise try to match them as closely as possible with the
96 * underlying file, i.e. in the best case we offer the same node type as the underlying node. */
98 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
99 _cleanup_free_
char *path
= NULL
;
101 path
= path_join(runtime_dir
, table
[i
].name
);
105 if (S_ISDIR(table
[i
].mode
))
106 r
= mkdir_label(path
, table
[i
].mode
& 07777);
108 r
= mknod_label(path
, table
[i
].mode
, makedev(0, 0));
111 log_debug_errno(r
, "Failed to create '%s', ignoring: %m", path
);
115 if (uid
!= UID_INVALID
|| gid
!= GID_INVALID
) {
116 if (lchown(path
, uid
, gid
) < 0)
117 log_debug_errno(errno
, "Failed to chown '%s': %m", path
);