]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
25 #include "alloc-util.h"
28 #include "nspawn-setuid.h"
29 #include "process-util.h"
30 #include "signal-util.h"
31 #include "string-util.h"
32 #include "user-util.h"
35 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
43 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
44 return log_error_errno(errno
, "Failed to allocate pipe: %m");
46 r
= safe_fork("(getent)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
51 char *empty_env
= NULL
;
53 if (dup3(pipe_fds
[1], STDOUT_FILENO
, 0) < 0)
57 safe_close(pipe_fds
[0]);
59 safe_close(pipe_fds
[1]);
61 nullfd
= open("/dev/null", O_RDWR
);
65 if (dup3(nullfd
, STDIN_FILENO
, 0) < 0)
68 if (dup3(nullfd
, STDERR_FILENO
, 0) < 0)
74 close_all_fds(NULL
, 0);
76 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
77 execle("/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
81 pipe_fds
[1] = safe_close(pipe_fds
[1]);
88 int change_uid_gid(const char *user
, char **_home
) {
89 char line
[LINE_MAX
], *x
, *u
, *g
, *h
;
90 const char *word
, *state
;
91 _cleanup_free_ uid_t
*uids
= NULL
;
92 _cleanup_free_
char *home
= NULL
;
93 _cleanup_fclose_
FILE *f
= NULL
;
94 _cleanup_close_
int fd
= -1;
104 if (!user
|| streq(user
, "root") || streq(user
, "0")) {
105 /* Reset everything fully to 0, just in case */
109 return log_error_errno(r
, "Failed to become root: %m");
115 /* First, get user credentials */
116 fd
= spawn_getent("passwd", user
, &pid
);
125 if (!fgets(line
, sizeof(line
), f
)) {
127 log_error("Failed to resolve user %s.", user
);
131 return log_error_errno(errno
, "Failed to read from getent: %m");
136 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
138 x
= strchr(line
, ':');
140 log_error("/etc/passwd entry has invalid user field.");
144 u
= strchr(x
+1, ':');
146 log_error("/etc/passwd entry has invalid password field.");
153 log_error("/etc/passwd entry has invalid UID field.");
161 log_error("/etc/passwd entry has invalid GID field.");
166 h
= strchr(x
+1, ':');
168 log_error("/etc/passwd entry has invalid GECOS field.");
175 log_error("/etc/passwd entry has invalid home directory field.");
181 r
= parse_uid(u
, &uid
);
183 log_error("Failed to parse UID of user.");
187 r
= parse_gid(g
, &gid
);
189 log_error("Failed to parse GID of user.");
197 /* Second, get group memberships */
198 fd
= spawn_getent("initgroups", user
, &pid
);
208 if (!fgets(line
, sizeof(line
), f
)) {
210 log_error("Failed to resolve user %s.", user
);
214 return log_error_errno(errno
, "Failed to read from getent: %m");
219 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
221 /* Skip over the username and subsequent separator whitespace */
223 x
+= strcspn(x
, WHITESPACE
);
224 x
+= strspn(x
, WHITESPACE
);
226 FOREACH_WORD(word
, l
, x
, state
) {
232 if (!GREEDY_REALLOC(uids
, sz
, n_uids
+1))
235 r
= parse_uid(c
, &uids
[n_uids
++]);
237 log_error("Failed to parse group data from getent.");
242 r
= mkdir_parents(home
, 0775);
244 return log_error_errno(r
, "Failed to make home root directory: %m");
246 r
= mkdir_safe(home
, 0755, uid
, gid
, false);
247 if (r
< 0 && r
!= -EEXIST
)
248 return log_error_errno(r
, "Failed to make home directory: %m");
250 (void) fchown(STDIN_FILENO
, uid
, gid
);
251 (void) fchown(STDOUT_FILENO
, uid
, gid
);
252 (void) fchown(STDERR_FILENO
, uid
, gid
);
254 if (setgroups(n_uids
, uids
) < 0)
255 return log_error_errno(errno
, "Failed to set auxiliary groups: %m");
257 if (setresgid(gid
, gid
, gid
) < 0)
258 return log_error_errno(errno
, "setresgid() failed: %m");
260 if (setresuid(uid
, uid
, uid
) < 0)
261 return log_error_errno(errno
, "setresuid() failed: %m");