]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
14 #include "nspawn-setuid.h"
15 #include "process-util.h"
16 #include "rlimit-util.h"
17 #include "signal-util.h"
18 #include "string-util.h"
20 #include "user-util.h"
23 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
31 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
32 return log_error_errno(errno
, "Failed to allocate pipe: %m");
34 r
= safe_fork("(getent)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
36 safe_close_pair(pipe_fds
);
40 char *empty_env
= NULL
;
42 safe_close(pipe_fds
[0]);
44 if (rearrange_stdio(-1, pipe_fds
[1], -1) < 0)
47 (void) close_all_fds(NULL
, 0);
49 (void) rlimit_nofile_safe();
51 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
52 execle("/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
56 pipe_fds
[1] = safe_close(pipe_fds
[1]);
63 int change_uid_gid_raw(
66 const gid_t
*supplementary_gids
,
67 size_t n_supplementary_gids
) {
69 if (!uid_is_valid(uid
))
71 if (!gid_is_valid(gid
))
74 (void) fchown(STDIN_FILENO
, uid
, gid
);
75 (void) fchown(STDOUT_FILENO
, uid
, gid
);
76 (void) fchown(STDERR_FILENO
, uid
, gid
);
78 if (setgroups(n_supplementary_gids
, supplementary_gids
) < 0)
79 return log_error_errno(errno
, "Failed to set auxiliary groups: %m");
81 if (setresgid(gid
, gid
, gid
) < 0)
82 return log_error_errno(errno
, "setresgid() failed: %m");
84 if (setresuid(uid
, uid
, uid
) < 0)
85 return log_error_errno(errno
, "setresuid() failed: %m");
90 int change_uid_gid(const char *user
, char **_home
) {
92 const char *word
, *state
;
93 _cleanup_free_ gid_t
*gids
= NULL
;
94 _cleanup_free_
char *home
= NULL
, *line
= NULL
;
95 _cleanup_fclose_
FILE *f
= NULL
;
96 _cleanup_close_
int fd
= -1;
106 if (!user
|| STR_IN_SET(user
, "root", "0")) {
107 /* Reset everything fully to 0, just in case */
111 return log_error_errno(r
, "Failed to become root: %m");
117 /* First, get user credentials */
118 fd
= spawn_getent("passwd", user
, &pid
);
127 r
= read_line(f
, LONG_LINE_MAX
, &line
);
129 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
130 "Failed to resolve user %s.", user
);
132 return log_error_errno(r
, "Failed to read from getent: %m");
134 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
136 x
= strchr(line
, ':');
138 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
139 "/etc/passwd entry has invalid user field.");
141 u
= strchr(x
+1, ':');
143 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
144 "/etc/passwd entry has invalid password field.");
149 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
150 "/etc/passwd entry has invalid UID field.");
156 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
157 "/etc/passwd entry has invalid GID field.");
160 h
= strchr(x
+1, ':');
162 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
163 "/etc/passwd entry has invalid GECOS field.");
168 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
169 "/etc/passwd entry has invalid home directory field.");
173 r
= parse_uid(u
, &uid
);
175 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
176 "Failed to parse UID of user.");
178 r
= parse_gid(g
, &gid
);
180 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
181 "Failed to parse GID of user.");
190 /* Second, get group memberships */
191 fd
= spawn_getent("initgroups", user
, &pid
);
200 r
= read_line(f
, LONG_LINE_MAX
, &line
);
202 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
203 "Failed to resolve user %s.", user
);
205 return log_error_errno(r
, "Failed to read from getent: %m");
207 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
209 /* Skip over the username and subsequent separator whitespace */
211 x
+= strcspn(x
, WHITESPACE
);
212 x
+= strspn(x
, WHITESPACE
);
214 FOREACH_WORD(word
, l
, x
, state
) {
220 if (!GREEDY_REALLOC(gids
, sz
, n_gids
+1))
223 r
= parse_gid(c
, &gids
[n_gids
++]);
225 return log_error_errno(r
, "Failed to parse group data from getent: %m");
228 r
= mkdir_parents(home
, 0775);
230 return log_error_errno(r
, "Failed to make home root directory: %m");
232 r
= mkdir_safe(home
, 0755, uid
, gid
, 0);
233 if (r
< 0 && !IN_SET(r
, -EEXIST
, -ENOTDIR
))
234 return log_error_errno(r
, "Failed to make home directory: %m");
236 r
= change_uid_gid_raw(uid
, gid
, gids
, n_gids
);
241 *_home
= TAKE_PTR(home
);