Debian Codegraph @

src/nspawn/nspawn-setuid.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "errno.h"
8 #include "extract-word.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "log.h"
12 #include "mkdir.h"
13 #include "nspawn-setuid.h"
14 #include "pidref.h"
15 #include "process-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "user-util.h"
19
20 static int spawn_getent(const char *database, const char *key, PidRef *ret) {
21 int pipe_fds[2], r;
22
23 assert(database);
24 assert(key);
25 assert(ret);
26
27 if (pipe2(pipe_fds, O_CLOEXEC) < 0)
28 return log_error_errno(errno, "Failed to allocate pipe: %m");
29
30 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
31 r = pidref_safe_fork_full(
32 "(getent)",
33 (int[]) { -EBADF, pipe_fds[1], -EBADF }, NULL, 0,
34 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE,
35 &pidref);
36 if (r < 0) {
37 safe_close_pair(pipe_fds);
38 return r;
39 }
40 if (r == 0) {
41 execlp("getent", "getent", database, key, NULL);
42 _exit(EXIT_FAILURE);
43 }
44
45 pipe_fds[1] = safe_close(pipe_fds[1]);
46
47 *ret = TAKE_PIDREF(pidref);
48
49 return pipe_fds[0];
50 }
51
52 int change_uid_gid_raw(
53 uid_t uid,
54 gid_t gid,
55 const gid_t *supplementary_gids,
56 size_t n_supplementary_gids,
57 bool chown_stdio) {
58
59 int r;
60
61 if (!uid_is_valid(uid))
62 uid = 0;
63 if (!gid_is_valid(gid))
64 gid = 0;
65
66 if (chown_stdio) {
67 (void) fchown(STDIN_FILENO, uid, gid);
68 (void) fchown(STDOUT_FILENO, uid, gid);
69 (void) fchown(STDERR_FILENO, uid, gid);
70 }
71
72 r = fully_set_uid_gid(uid, gid, supplementary_gids, n_supplementary_gids);
73 if (r < 0)
74 return log_error_errno(r, "Changing privileges failed: %m");
75
76 return 0;
77 }
78
79 int change_uid_gid(const char *user, bool chown_stdio, char **ret_home) {
80 char *x, *u, *g, *h;
81 _cleanup_free_ gid_t *gids = NULL;
82 _cleanup_free_ char *home = NULL, *line = NULL;
83 _cleanup_fclose_ FILE *f = NULL;
84 _cleanup_close_ int fd = -EBADF;
85 unsigned n_gids = 0;
86 uid_t uid;
87 gid_t gid;
88 int r;
89
90 assert(ret_home);
91
92 if (!user || STR_IN_SET(user, "root", "0")) {
93 /* Reset everything fully to 0, just in case */
94
95 r = reset_uid_gid();
96 if (r < 0)
97 return log_error_errno(r, "Failed to become root: %m");
98
99 *ret_home = NULL;
100 return 0;
101 }
102
103 /* First, get user credentials */
104 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
105 fd = spawn_getent("passwd", user, &pidref);
106 if (fd < 0)
107 return fd;
108
109 f = take_fdopen(&fd, "r");
110 if (!f)
111 return log_oom();
112
113 r = read_line(f, LONG_LINE_MAX, &line);
114 if (r == 0)
115 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
116 "Failed to resolve user %s.", user);
117 if (r < 0)
118 return log_error_errno(r, "Failed to read from getent: %m");
119
120 (void) pidref_wait_for_terminate_and_check("getent passwd", &pidref, WAIT_LOG);
121
122 x = strchr(line, ':');
123 if (!x)
124 return log_error_errno(SYNTHETIC_ERRNO(EIO),
125 "/etc/passwd entry has invalid user field.");
126
127 u = strchr(x+1, ':');
128 if (!u)
129 return log_error_errno(SYNTHETIC_ERRNO(EIO),
130 "/etc/passwd entry has invalid password field.");
131
132 u++;
133 g = strchr(u, ':');
134 if (!g)
135 return log_error_errno(SYNTHETIC_ERRNO(EIO),
136 "/etc/passwd entry has invalid UID field.");
137
138 *g = 0;
139 g++;
140 x = strchr(g, ':');
141 if (!x)
142 return log_error_errno(SYNTHETIC_ERRNO(EIO),
143 "/etc/passwd entry has invalid GID field.");
144
145 *x = 0;
146 h = strchr(x+1, ':');
147 if (!h)
148 return log_error_errno(SYNTHETIC_ERRNO(EIO),
149 "/etc/passwd entry has invalid GECOS field.");
150
151 h++;
152 x = strchr(h, ':');
153 if (!x)
154 return log_error_errno(SYNTHETIC_ERRNO(EIO),
155 "/etc/passwd entry has invalid home directory field.");
156
157 *x = 0;
158
159 r = parse_uid(u, &uid);
160 if (r < 0)
161 return log_error_errno(SYNTHETIC_ERRNO(EIO),
162 "Failed to parse UID of user.");
163
164 r = parse_gid(g, &gid);
165 if (r < 0)
166 return log_error_errno(SYNTHETIC_ERRNO(EIO),
167 "Failed to parse GID of user.");
168
169 home = strdup(h);
170 if (!home)
171 return log_oom();
172
173 f = safe_fclose(f);
174 line = mfree(line);
175
176 /* Second, get group memberships */
177 pidref_done(&pidref);
178 fd = spawn_getent("initgroups", user, &pidref);
179 if (fd < 0)
180 return fd;
181
182 f = take_fdopen(&fd, "r");
183 if (!f)
184 return log_oom();
185
186 r = read_line(f, LONG_LINE_MAX, &line);
187 if (r == 0)
188 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
189 "Failed to resolve user %s.", user);
190 if (r < 0)
191 return log_error_errno(r, "Failed to read from getent: %m");
192
193 (void) pidref_wait_for_terminate_and_check("getent initgroups", &pidref, WAIT_LOG);
194
195 /* Skip over the username and subsequent separator whitespace */
196 x = line;
197 x += strcspn(x, WHITESPACE);
198 x += strspn(x, WHITESPACE);
199
200 for (const char *p = x;;) {
201 _cleanup_free_ char *word = NULL;
202
203 r = extract_first_word(&p, &word, NULL, 0);
204 if (r < 0)
205 return log_error_errno(r, "Failed to parse group data from getent: %m");
206 if (r == 0)
207 break;
208
209 if (!GREEDY_REALLOC(gids, n_gids+1))
210 return log_oom();
211
212 r = parse_gid(word, &gids[n_gids++]);
213 if (r < 0)
214 return log_error_errno(r, "Failed to parse group data from getent: %m");
215 }
216
217 r = mkdir_parents(home, 0775);
218 if (r < 0)
219 return log_error_errno(r, "Failed to make home root directory: %m");
220
221 r = mkdir_safe(home, 0755, uid, gid, 0);
222 if (r < 0 && !IN_SET(r, -EEXIST, -ENOTDIR))
223 return log_error_errno(r, "Failed to make home directory: %m");
224
225 r = change_uid_gid_raw(uid, gid, gids, n_gids, chown_stdio);
226 if (r < 0)
227 return r;
228
229 if (ret_home)
230 *ret_home = TAKE_PTR(home);
231
232 return 0;
233 }