]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dev-setup.c
e79db91ddee5339ae3faa0e7a1b233df9f05761d
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <sys/sysmacros.h>
6 #include "alloc-util.h"
10 #include "label-util.h"
12 #include "mkdir-label.h"
13 #include "nulstr-util.h"
14 #include "path-util.h"
15 #include "stat-util.h"
16 #include "umask-util.h"
17 #include "user-util.h"
19 int dev_setup(const char *prefix
, uid_t uid
, gid_t gid
) {
20 static const char symlinks
[] =
21 "-/proc/kcore\0" "/dev/core\0"
22 "/proc/self/fd\0" "/dev/fd\0"
23 "/proc/self/fd/0\0" "/dev/stdin\0"
24 "/proc/self/fd/1\0" "/dev/stdout\0"
25 "/proc/self/fd/2\0" "/dev/stderr\0";
29 NULSTR_FOREACH_PAIR(j
, k
, symlinks
) {
30 _cleanup_free_
char *link_name
= NULL
;
36 if (access(j
, F_OK
) < 0)
41 link_name
= path_join(prefix
, k
);
49 r
= symlink_label(j
, n
);
51 log_debug_errno(r
, "Failed to symlink %s to %s: %m", j
, n
);
53 if (uid
!= UID_INVALID
|| gid
!= GID_INVALID
)
54 if (lchown(n
, uid
, gid
) < 0)
55 log_debug_errno(errno
, "Failed to chown %s: %m", n
);
61 int make_inaccessible_nodes(
62 const char *parent_dir
,
66 static const mode_t table
[] = {
72 /* The following two are likely to fail if we lack the privs for it (for example in an userns
73 * environment, if CAP_SYS_MKNOD is missing, or if a device node policy prohibits creation of
74 * device nodes with a major/minor of 0). But that's entirely fine. Consumers of these files
75 * should implement falling back to use a different node then, for example
76 * <root>/inaccessible/sock, which is close enough in behaviour and semantics for most uses.
81 /* NB: S_IFLNK is not listed here, as there is no such thing as an inaccessible symlink */
84 _cleanup_close_
int parent_fd
= -EBADF
, inaccessible_fd
= -EBADF
;
88 parent_dir
= "/run/systemd";
90 BLOCK_WITH_UMASK(0000);
92 parent_fd
= open(parent_dir
, O_DIRECTORY
|O_CLOEXEC
|O_PATH
, 0);
96 inaccessible_fd
= open_mkdir_at_full(parent_fd
, "inaccessible", O_CLOEXEC
, XO_LABEL
, 0755);
97 if (inaccessible_fd
< 0)
98 return inaccessible_fd
;
100 /* Set up inaccessible (and empty) file nodes of all types. This are used to as mount sources for over-mounting
101 * ("masking") file nodes that shall become inaccessible and empty for specific containers or services. We try
102 * to lock down these nodes as much as we can, but otherwise try to match them as closely as possible with the
103 * underlying file, i.e. in the best case we offer the same node type as the underlying node. */
105 FOREACH_ELEMENT(m
, table
) {
106 _cleanup_free_
char *path
= NULL
;
107 mode_t inode_type
= *m
;
110 fn
= inode_type_to_string(inode_type
);
111 path
= path_join(parent_dir
, fn
);
115 if (S_ISDIR(inode_type
))
116 r
= mkdirat_label(inaccessible_fd
, fn
, 0000);
118 r
= mknodat_label(inaccessible_fd
, fn
, inode_type
| 0000, makedev(0, 0));
120 if (fchmodat(inaccessible_fd
, fn
, 0000, AT_SYMLINK_NOFOLLOW
) < 0)
121 log_debug_errno(errno
, "Failed to adjust access mode of existing inode '%s', ignoring: %m", path
);
123 log_debug_errno(r
, "Failed to create '%s', ignoring: %m", path
);
127 if (uid_is_valid(uid
) || gid_is_valid(gid
))
128 if (fchownat(inaccessible_fd
, fn
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
129 log_debug_errno(errno
, "Failed to chown '%s', ignoring: %m", path
);
132 if (fchmod(inaccessible_fd
, 0555) < 0)
133 log_debug_errno(errno
, "Failed to mark inaccessible directory read-only, ignoring: %m");