]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
13 #include "nspawn-setuid.h"
14 #include "process-util.h"
15 #include "rlimit-util.h"
16 #include "signal-util.h"
17 #include "string-util.h"
19 #include "user-util.h"
22 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
30 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
31 return log_error_errno(errno
, "Failed to allocate pipe: %m");
33 r
= safe_fork("(getent)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
35 safe_close_pair(pipe_fds
);
39 char *empty_env
= NULL
;
41 safe_close(pipe_fds
[0]);
43 if (rearrange_stdio(-1, pipe_fds
[1], -1) < 0)
46 close_all_fds(NULL
, 0);
48 (void) rlimit_nofile_safe();
50 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
51 execle("/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
55 pipe_fds
[1] = safe_close(pipe_fds
[1]);
62 int change_uid_gid(const char *user
, char **_home
) {
64 const char *word
, *state
;
65 _cleanup_free_ uid_t
*uids
= NULL
;
66 _cleanup_free_
char *home
= NULL
, *line
= NULL
;
67 _cleanup_fclose_
FILE *f
= NULL
;
68 _cleanup_close_
int fd
= -1;
78 if (!user
|| STR_IN_SET(user
, "root", "0")) {
79 /* Reset everything fully to 0, just in case */
83 return log_error_errno(r
, "Failed to become root: %m");
89 /* First, get user credentials */
90 fd
= spawn_getent("passwd", user
, &pid
);
99 r
= read_line(f
, LONG_LINE_MAX
, &line
);
101 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
102 "Failed to resolve user %s.", user
);
104 return log_error_errno(r
, "Failed to read from getent: %m");
106 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
108 x
= strchr(line
, ':');
110 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
111 "/etc/passwd entry has invalid user field.");
113 u
= strchr(x
+1, ':');
115 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
116 "/etc/passwd entry has invalid password field.");
121 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
122 "/etc/passwd entry has invalid UID field.");
128 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
129 "/etc/passwd entry has invalid GID field.");
132 h
= strchr(x
+1, ':');
134 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
135 "/etc/passwd entry has invalid GECOS field.");
140 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
141 "/etc/passwd entry has invalid home directory field.");
145 r
= parse_uid(u
, &uid
);
147 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
148 "Failed to parse UID of user.");
150 r
= parse_gid(g
, &gid
);
152 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
153 "Failed to parse GID of user.");
162 /* Second, get group memberships */
163 fd
= spawn_getent("initgroups", user
, &pid
);
167 f
= fdopen(fd
, "re");
172 r
= read_line(f
, LONG_LINE_MAX
, &line
);
174 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
175 "Failed to resolve user %s.", user
);
177 return log_error_errno(r
, "Failed to read from getent: %m");
179 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
181 /* Skip over the username and subsequent separator whitespace */
183 x
+= strcspn(x
, WHITESPACE
);
184 x
+= strspn(x
, WHITESPACE
);
186 FOREACH_WORD(word
, l
, x
, state
) {
192 if (!GREEDY_REALLOC(uids
, sz
, n_uids
+1))
195 r
= parse_uid(c
, &uids
[n_uids
++]);
197 return log_error_errno(r
, "Failed to parse group data from getent: %m");
200 r
= mkdir_parents(home
, 0775);
202 return log_error_errno(r
, "Failed to make home root directory: %m");
204 r
= mkdir_safe(home
, 0755, uid
, gid
, 0);
205 if (r
< 0 && !IN_SET(r
, -EEXIST
, -ENOTDIR
))
206 return log_error_errno(r
, "Failed to make home directory: %m");
208 (void) fchown(STDIN_FILENO
, uid
, gid
);
209 (void) fchown(STDOUT_FILENO
, uid
, gid
);
210 (void) fchown(STDERR_FILENO
, uid
, gid
);
212 if (setgroups(n_uids
, uids
) < 0)
213 return log_error_errno(errno
, "Failed to set auxiliary groups: %m");
215 if (setresgid(gid
, gid
, gid
) < 0)
216 return log_error_errno(errno
, "setresgid() failed: %m");
218 if (setresuid(uid
, uid
, uid
) < 0)
219 return log_error_errno(errno
, "setresuid() failed: %m");
222 *_home
= TAKE_PTR(home
);