Debian Codegraph @

src/nspawn/nspawn-bind-user.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-json.h"
6
7 #include "alloc-util.h"
8 #include "chase.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "format-util.h"
12 #include "io-util.h"
13 #include "log.h"
14 #include "nspawn.h"
15 #include "machine-bind-user.h"
16 #include "nspawn-bind-user.h"
17 #include "strv.h"
18 #include "user-record.h"
19 #include "group-record.h"
20 #include "path-util.h"
21 #include "string-util.h"
22 #include "user-util.h"
23
24 static int write_and_symlink(
25 const char *root,
26 sd_json_variant *v,
27 const char *name,
28 uid_t uid,
29 const char *suffix,
30 WriteStringFileFlags extra_flags) {
31
32 _cleanup_free_ char *j = NULL, *f = NULL, *p = NULL, *q = NULL;
33 int r;
34
35 assert(root);
36 assert(v);
37 assert(name);
38 assert(uid_is_valid(uid));
39 assert(suffix);
40
41 r = sd_json_variant_format(v, SD_JSON_FORMAT_NEWLINE, &j);
42 if (r < 0)
43 return log_error_errno(r, "Failed to format user record JSON: %m");
44
45 f = strjoin(name, suffix);
46 if (!f)
47 return log_oom();
48
49 p = path_join(root, "/run/host/userdb/", f);
50 if (!p)
51 return log_oom();
52
53 if (asprintf(&q, "%s/run/host/userdb/" UID_FMT "%s", root, uid, suffix) < 0)
54 return log_oom();
55
56 if (symlink(f, q) < 0)
57 return log_error_errno(errno, "Failed to create symlink '%s': %m", q);
58
59 r = userns_lchown(q, 0, 0);
60 if (r < 0)
61 return log_error_errno(r, "Failed to adjust access mode of '%s': %m", q);
62
63 r = write_string_file(p, j, WRITE_STRING_FILE_CREATE|extra_flags);
64 if (r < 0)
65 return log_error_errno(r, "Failed to write %s: %m", p);
66
67 r = userns_lchown(p, 0, 0);
68 if (r < 0)
69 return log_error_errno(r, "Failed to adjust access mode of '%s': %m", p);
70
71 return 0;
72 }
73
74 static int write_membership(const char *root, const char *user, const char *group) {
75 int r;
76
77 assert(user);
78 assert(group);
79
80 _cleanup_free_ char *membership = strjoin(user, ":", group, ".membership");
81 if (!membership)
82 return log_oom();
83
84 _cleanup_free_ char *p = path_join("/run/host/userdb/", membership);
85 if (!p)
86 return log_oom();
87
88 _cleanup_close_ int fd = chase_and_open(
89 p,
90 root,
91 CHASE_PREFIX_ROOT|CHASE_NO_AUTOFS,
92 O_WRONLY|O_CREAT|O_CLOEXEC,
93 /* ret_path= */ NULL);
94 if (fd < 0)
95 return log_error_errno(errno, "Failed to create %s: %m", p);
96
97 r = userns_chown_at(fd, /* fname= */ NULL, /* uid= */ 0, /* gid= */ 0, /* flags= */ 0);
98 if (r < 0)
99 return log_error_errno(r, "Failed to adjust access mode of '%s': %m", p);
100
101 r = loop_write(fd, "{}\n", SIZE_MAX);
102 if (r < 0)
103 return log_error_errno(r, "Failed to write empty JSON object into %s: %m", p);
104
105 return 0;
106 }
107
108 int bind_user_setup(const MachineBindUserContext *c, const char *root) {
109 static const UserRecordLoadFlags strip_flags = /* Removes privileged info */
110 USER_RECORD_LOAD_MASK_PRIVILEGED|
111 USER_RECORD_PERMISSIVE;
112 static const UserRecordLoadFlags shadow_flags = /* Extracts privileged info */
113 USER_RECORD_EXTRACT_PRIVILEGED|
114 USER_RECORD_EMPTY_OK|
115 USER_RECORD_PERMISSIVE;
116 int r;
117
118 assert(root);
119
120 if (!c || c->n_data == 0)
121 return 0;
122
123 r = make_run_host(root);
124 if (r < 0)
125 return r;
126
127 r = userns_mkdir(root, "/run/host/home", 0755, 0, 0);
128 if (r < 0)
129 return log_error_errno(r, "Failed to create /run/host/home: %m");
130
131 r = userns_mkdir(root, "/run/host/userdb", 0755, 0, 0);
132 if (r < 0)
133 return log_error_errno(r, "Failed to create /run/host/userdb: %m");
134
135 FOREACH_ARRAY(d, c->data, c->n_data) {
136 _cleanup_(group_record_unrefp) GroupRecord *stripped_group = NULL, *shadow_group = NULL;
137 _cleanup_(user_record_unrefp) UserRecord *stripped_user = NULL, *shadow_user = NULL;
138
139 /* First, write shadow (i.e. privileged) data for group record */
140 r = group_record_clone(d->payload_group, shadow_flags, &shadow_group);
141 if (r < 0)
142 return log_error_errno(r, "Failed to extract privileged information from group record: %m");
143
144 if (!sd_json_variant_is_blank_object(shadow_group->json)) {
145 r = write_and_symlink(
146 root,
147 shadow_group->json,
148 d->payload_group->group_name,
149 d->payload_group->gid,
150 ".group-privileged",
151 WRITE_STRING_FILE_MODE_0600);
152 if (r < 0)
153 return r;
154 }
155
156 /* Second, write main part of group record. */
157 r = group_record_clone(d->payload_group, strip_flags, &stripped_group);
158 if (r < 0)
159 return log_error_errno(r, "Failed to strip privileged information from group record: %m");
160
161 r = write_and_symlink(
162 root,
163 stripped_group->json,
164 d->payload_group->group_name,
165 d->payload_group->gid,
166 ".group",
167 0);
168 if (r < 0)
169 return r;
170
171 STRV_FOREACH(u, stripped_group->members) {
172 r = write_membership(root, *u, stripped_group->group_name);
173 if (r < 0)
174 return r;
175 }
176
177 /* Third, write out user shadow data. i.e. extract privileged info from user record */
178 r = user_record_clone(d->payload_user, shadow_flags, &shadow_user);
179 if (r < 0)
180 return log_error_errno(r, "Failed to extract privileged information from user record: %m");
181
182 if (!sd_json_variant_is_blank_object(shadow_user->json)) {
183 r = write_and_symlink(
184 root,
185 shadow_user->json,
186 d->payload_user->user_name,
187 d->payload_user->uid,
188 ".user-privileged",
189 WRITE_STRING_FILE_MODE_0600);
190 if (r < 0)
191 return r;
192 }
193
194 /* Finally write out the main part of the user record */
195 r = user_record_clone(d->payload_user, strip_flags, &stripped_user);
196 if (r < 0)
197 return log_error_errno(r, "Failed to strip privileged information from user record: %m");
198
199 r = write_and_symlink(
200 root,
201 stripped_user->json,
202 d->payload_user->user_name,
203 d->payload_user->uid,
204 ".user",
205 0);
206 if (r < 0)
207 return r;
208
209 STRV_FOREACH(g, stripped_user->member_of) {
210 r = write_membership(root, stripped_user->user_name, *g);
211 if (r < 0)
212 return r;
213 }
214 }
215
216 return 1;
217 }