Debian Codegraph @

lib/acl.c

raw symbols SCIP
1 /*
2 * POSIX ACL get/set/delete via the generic xattr syscalls.
3 *
4 * POSIX ACLs are stored by the kernel as the "system.posix_acl_access" and
5 * "system.posix_acl_default" extended attributes, in a fixed little-endian
6 * wire format (see include/acl_ea.h in the acl package). By serializing that
7 * format ourselves and using fgetxattr/fsetxattr on a held O_NOFOLLOW fd -- or
8 * getxattrat/setxattrat(AT_SYMLINK_NOFOLLOW) on a dirfd+leaf -- we get a
9 * symlink-race-safe ACL primitive that also covers the *default* ACL, which
10 * libacl's fd API (acl_get_fd/acl_set_fd, access-only) cannot.
11 *
12 * This file knows nothing about rsync's globals or its internal ACL form: it
13 * speaks a neutral (tag, perm, id) entry array, which makes it directly
14 * comparable against the system libacl in the t_acl unit test.
15 *
16 * Copyright (C) 2026 Wayne Davison & the rsync project
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, visit the http://fsf.org website.
30 */
31
32 #include "rsync.h"
33 #include "acl.h"
34
35 #ifdef SUPPORT_ACL_FD
36
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <fcntl.h> /* AT_SYMLINK_NOFOLLOW */
42
43 #if defined HAVE_SYS_XATTR_H
44 #include <sys/xattr.h>
45 #elif defined HAVE_ATTR_XATTR_H
46 #include <attr/xattr.h>
47 #endif
48
49 #ifdef HAVE_XATTRAT_SYSCALLS
50 #include <sys/syscall.h>
51 /* Self-contained copy of the kernel's struct xattr_args (stable ABI: an
52 * 8-byte-aligned u64 pointer, then two u32s). Defined locally to avoid
53 * pulling <linux/xattr.h>, whose XATTR_* macros clash with <sys/xattr.h>. */
54 struct rsync_xattr_args {
55 uint64_t value __attribute__((aligned(8)));
56 uint32_t size;
57 uint32_t flags;
58 };
59 #endif
60
61 /* Linux 2.4 didn't have a distinct ENOATTR. */
62 #ifndef ENOATTR
63 #define ENOATTR ENODATA
64 #endif
65
66 #define ACL_XATTR_ACCESS "system.posix_acl_access"
67 #define ACL_XATTR_DEFAULT "system.posix_acl_default"
68
69 /* On-disk layout: a 4-byte LE version header followed by 8-byte LE entries. */
70 #define ACL_EA_VERSION 0x0002
71 #define ACL_EA_HDR_LEN 4
72 #define ACL_EA_ENT_LEN 8
73
74 /* === little-endian (de)serialization (host-endianness independent) === */
75
76 static void put_le16(unsigned char *p, uint16_t v)
77 {
78 p[0] = (unsigned char)(v & 0xff);
79 p[1] = (unsigned char)((v >> 8) & 0xff);
80 }
81
82 static void put_le32(unsigned char *p, uint32_t v)
83 {
84 p[0] = (unsigned char)(v & 0xff);
85 p[1] = (unsigned char)((v >> 8) & 0xff);
86 p[2] = (unsigned char)((v >> 16) & 0xff);
87 p[3] = (unsigned char)((v >> 24) & 0xff);
88 }
89
90 static uint16_t get_le16(const unsigned char *p)
91 {
92 return (uint16_t)(p[0] | (p[1] << 8));
93 }
94
95 static uint32_t get_le32(const unsigned char *p)
96 {
97 return (uint32_t)p[0] | ((uint32_t)p[1] << 8)
98 | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
99 }
100
101 static int is_named_tag(uint16_t tag)
102 {
103 return tag == RACL_USER || tag == RACL_GROUP;
104 }
105
106 /* Canonical order: tag ascending, then id ascending within a tag. This is
107 * the order libacl's __acl_reorder_obj_p() produces and what the kernel's
108 * validator expects (USER_OBJ, USER*, GROUP_OBJ, GROUP*, MASK, OTHER). */
109 static int ent_compare(const void *a, const void *b)
110 {
111 const rsync_acl_ent *x = a, *y = b;
112 if (x->tag != y->tag)
113 return x->tag < y->tag ? -1 : 1;
114 if (x->id != y->id)
115 return x->id < y->id ? -1 : 1;
116 return 0;
117 }
118
119 /* Serialize entries into a freshly-malloc'd xattr buffer (canonical order). */
120 static unsigned char *acl_to_xattr(const rsync_acl_ent *ents, int count, size_t *len_out)
121 {
122 size_t len = ACL_EA_HDR_LEN + (size_t)count * ACL_EA_ENT_LEN;
123 unsigned char *buf = malloc(len);
124 rsync_acl_ent *sorted = NULL;
125 unsigned char *p;
126 int i;
127
128 if (!buf)
129 return NULL;
130 if (count > 1) {
131 sorted = malloc((size_t)count * sizeof sorted[0]);
132 if (!sorted) {
133 free(buf);
134 return NULL;
135 }
136 memcpy(sorted, ents, (size_t)count * sizeof sorted[0]);
137 qsort(sorted, count, sizeof sorted[0], ent_compare);
138 ents = sorted;
139 }
140
141 put_le32(buf, ACL_EA_VERSION);
142 p = buf + ACL_EA_HDR_LEN;
143 for (i = 0; i < count; i++, p += ACL_EA_ENT_LEN) {
144 put_le16(p, ents[i].tag);
145 put_le16(p + 2, ents[i].perm);
146 put_le32(p + 4, is_named_tag(ents[i].tag) ? ents[i].id : RACL_UNDEFINED_ID);
147 }
148
149 if (sorted)
150 free(sorted);
151 *len_out = len;
152 return buf;
153 }
154
155 /* Parse an xattr buffer into a malloc'd entry array (canonical order). */
156 static int xattr_to_acl(const unsigned char *buf, size_t len,
157 rsync_acl_ent **out, int *count_out)
158 {
159 rsync_acl_ent *ents;
160 const unsigned char *p;
161 int n, i;
162
163 if (len < ACL_EA_HDR_LEN || (len - ACL_EA_HDR_LEN) % ACL_EA_ENT_LEN != 0
164 || get_le32(buf) != ACL_EA_VERSION) {
165 errno = EINVAL;
166 return -1;
167 }
168 n = (int)((len - ACL_EA_HDR_LEN) / ACL_EA_ENT_LEN);
169
170 ents = n ? malloc((size_t)n * sizeof ents[0]) : NULL;
171 if (n && !ents)
172 return -1;
173 p = buf + ACL_EA_HDR_LEN;
174 for (i = 0; i < n; i++, p += ACL_EA_ENT_LEN) {
175 ents[i].tag = get_le16(p);
176 ents[i].perm = get_le16(p + 2);
177 ents[i].id = is_named_tag(ents[i].tag) ? get_le32(p + 4) : RACL_UNDEFINED_ID;
178 }
179 if (n > 1)
180 qsort(ents, n, sizeof ents[0], ent_compare);
181
182 *out = ents;
183 *count_out = n;
184 return 0;
185 }
186
187 /* === syscall dispatchers (fd-variant vs at-variant) === */
188
189 /* Pre-6.13 fallback for the dirfd+leaf at-variants: address the leaf as
190 * /proc/self/fd/<dirfd>/<leaf> and use the l*xattr (no-follow-leaf) calls. The
191 * /proc/self/fd/<dirfd> magic symlink resolves to the pinned parent inode -- a
192 * raced parent symlink cannot redirect it -- and l*xattr does not follow a raced
193 * leaf symlink, so this is race-safe without the Linux 6.13 *xattrat syscalls, as
194 * long as procfs is mounted. (`leaf` is a single component, <= NAME_MAX.)
195 * Returns 0 and fills `buf`, or -1 with ENAMETOOLONG. */
196 static int proc_fd_leaf_path(char *buf, size_t buflen, int dirfd, const char *leaf)
197 {
198 int n = snprintf(buf, buflen, "/proc/self/fd/%d/%s", dirfd, leaf);
199 if (n < 0 || (size_t)n >= buflen) {
200 errno = ENAMETOOLONG;
201 return -1;
202 }
203 return 0;
204 }
205
206 static ssize_t do_getxattr(int fd, int dirfd, const char *leaf,
207 const char *name, void *val, size_t size)
208 {
209 char p[MAXPATHLEN];
210
211 if (fd >= 0)
212 return fgetxattr(fd, name, val, size);
213 #ifdef HAVE_XATTRAT_SYSCALLS
214 {
215 struct rsync_xattr_args args;
216 ssize_t ret;
217 args.value = (uint64_t)(uintptr_t)val;
218 args.size = (uint32_t)size;
219 args.flags = 0;
220 ret = syscall(SYS_getxattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW,
221 name, &args, sizeof args);
222 if (ret != -1 || errno != ENOSYS)
223 return ret;
224 /* ENOSYS: kernel < 6.13 -- fall through to the /proc compat. */
225 }
226 #endif
227 if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0)
228 return -1;
229 return lgetxattr(p, name, val, size);
230 }
231
232 static int do_setxattr(int fd, int dirfd, const char *leaf,
233 const char *name, const void *val, size_t size)
234 {
235 char p[MAXPATHLEN];
236
237 if (fd >= 0)
238 return fsetxattr(fd, name, val, size, 0);
239 #ifdef HAVE_XATTRAT_SYSCALLS
240 {
241 struct rsync_xattr_args args;
242 int ret;
243 args.value = (uint64_t)(uintptr_t)val;
244 args.size = (uint32_t)size;
245 args.flags = 0; /* replace */
246 ret = syscall(SYS_setxattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW,
247 name, &args, sizeof args);
248 if (ret != -1 || errno != ENOSYS)
249 return ret;
250 }
251 #endif
252 if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0)
253 return -1;
254 return lsetxattr(p, name, val, size, 0);
255 }
256
257 static int do_removexattr(int fd, int dirfd, const char *leaf, const char *name)
258 {
259 char p[MAXPATHLEN];
260
261 if (fd >= 0)
262 return fremovexattr(fd, name);
263 #ifdef HAVE_XATTRAT_SYSCALLS
264 {
265 int ret = syscall(SYS_removexattrat, dirfd, leaf, AT_SYMLINK_NOFOLLOW, name);
266 if (ret != -1 || errno != ENOSYS)
267 return ret;
268 }
269 #endif
270 if (proc_fd_leaf_path(p, sizeof p, dirfd, leaf) < 0)
271 return -1;
272 return lremovexattr(p, name);
273 }
274
275 /* Read the whole named xattr into a malloc'd buffer, growing as needed. */
276 static int read_full_xattr(int fd, int dirfd, const char *leaf,
277 const char *name, unsigned char **buf_out, size_t *len_out)
278 {
279 unsigned char *buf = NULL;
280 size_t size = 0;
281 int tries;
282
283 for (tries = 0; tries < 8; tries++) {
284 ssize_t n = do_getxattr(fd, dirfd, leaf, name, size ? buf : NULL, size);
285 if (n >= 0) {
286 if (size == 0) {
287 /* First call just learned the length. */
288 size = n ? (size_t)n : 1;
289 buf = malloc(size);
290 if (!buf)
291 return -1;
292 continue;
293 }
294 *buf_out = buf;
295 *len_out = (size_t)n;
296 return 0;
297 }
298 if (errno == ERANGE) { /* grew under us: re-probe the size */
299 if (buf)
300 free(buf);
301 buf = NULL;
302 size = 0;
303 continue;
304 }
305 if (buf)
306 free(buf);
307 return -1; /* ENODATA / EOPNOTSUPP / ENOSYS / ... in errno */
308 }
309 if (buf)
310 free(buf);
311 errno = ERANGE;
312 return -1;
313 }
314
315 /* === public API === */
316
317 static int acl_get_common(int fd, int dirfd, const char *leaf,
318 int want_default, rsync_acl_ent **entries, int *count)
319 {
320 const char *name = want_default ? ACL_XATTR_DEFAULT : ACL_XATTR_ACCESS;
321 unsigned char *buf;
322 size_t len;
323 int rc;
324
325 *entries = NULL;
326 *count = 0;
327
328 if (read_full_xattr(fd, dirfd, leaf, name, &buf, &len) < 0) {
329 if (errno == ENODATA || errno == ENOATTR)
330 return 0; /* no explicit ACL present */
331 return -1; /* EOPNOTSUPP / ENOSYS / real error */
332 }
333
334 rc = xattr_to_acl(buf, len, entries, count);
335 free(buf);
336 return rc;
337 }
338
339 int xacl_get_fd(int fd, int want_default, rsync_acl_ent **entries, int *count)
340 {
341 return acl_get_common(fd, -1, NULL, want_default, entries, count);
342 }
343
344 int xacl_get_at(int dirfd, const char *leaf, int want_default,
345 rsync_acl_ent **entries, int *count)
346 {
347 return acl_get_common(-1, dirfd, leaf, want_default, entries, count);
348 }
349
350 static int acl_set_common(int fd, int dirfd, const char *leaf,
351 int want_default, const rsync_acl_ent *ents, int count)
352 {
353 const char *name = want_default ? ACL_XATTR_DEFAULT : ACL_XATTR_ACCESS;
354 unsigned char *buf;
355 size_t len;
356 int rc, save_errno;
357
358 buf = acl_to_xattr(ents, count, &len);
359 if (!buf) {
360 errno = ENOMEM;
361 return -1;
362 }
363 rc = do_setxattr(fd, dirfd, leaf, name, buf, len);
364 save_errno = errno;
365 free(buf);
366 errno = save_errno;
367 return rc < 0 ? -1 : 0;
368 }
369
370 int xacl_set_fd(int fd, int want_default, const rsync_acl_ent *ents, int count)
371 {
372 return acl_set_common(fd, -1, NULL, want_default, ents, count);
373 }
374
375 int xacl_set_at(int dirfd, const char *leaf, int want_default,
376 const rsync_acl_ent *ents, int count)
377 {
378 return acl_set_common(-1, dirfd, leaf, want_default, ents, count);
379 }
380
381 static int acl_del_default_common(int fd, int dirfd, const char *leaf)
382 {
383 if (do_removexattr(fd, dirfd, leaf, ACL_XATTR_DEFAULT) < 0) {
384 if (errno == ENODATA || errno == ENOATTR)
385 return 0; /* already absent: success, like acl_delete_def_file */
386 return -1;
387 }
388 return 0;
389 }
390
391 int xacl_del_default_fd(int fd)
392 {
393 return acl_del_default_common(fd, -1, NULL);
394 }
395
396 int xacl_del_default_at(int dirfd, const char *leaf)
397 {
398 return acl_del_default_common(-1, dirfd, leaf);
399 }
400
401 /* True iff /proc/self/fd magic symlinks are usable, so the dirfd+leaf at-variants
402 * work race-safely via the /proc compat on a pre-6.13 kernel. */
403 static int proc_self_fd_usable(void)
404 {
405 int dfd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
406 char p[64];
407 int usable = 0;
408
409 if (dfd < 0)
410 return 0;
411 if (snprintf(p, sizeof p, "/proc/self/fd/%d/.", dfd) < (int)sizeof p) {
412 /* The probe attr is absent; the path resolving (any errno but
413 * ENOENT/ENOTDIR -- e.g. ENODATA/ENOTSUP/EACCES) means procfs gives us
414 * the magic fd-symlink we need. */
415 errno = 0;
416 lgetxattr(p, "user.rsync_acl_probe", NULL, 0);
417 usable = !(errno == ENOENT || errno == ENOTDIR);
418 }
419 close(dfd);
420 return usable;
421 }
422
423 int xacl_at_available(void)
424 {
425 static int avail = -1;
426
427 if (avail < 0) {
428 #ifdef HAVE_XATTRAT_SYSCALLS
429 /* Probe the *xattrat syscall directly (not via do_getxattr's /proc
430 * fallback): any errno other than ENOSYS means it is present (6.13+). */
431 struct rsync_xattr_args args;
432 args.value = 0;
433 args.size = 0;
434 args.flags = 0;
435 errno = 0;
436 syscall(SYS_getxattrat, AT_FDCWD, ".", AT_SYMLINK_NOFOLLOW,
437 "user.rsync_acl_probe", &args, sizeof args);
438 if (errno != ENOSYS) {
439 avail = 1;
440 return avail;
441 }
442 #endif
443 /* No *xattrat syscalls (pre-6.13, or a kernel built without them): the dirfd+leaf ACL ops
444 * are still race-safe via /proc/self/fd if procfs is mounted, closing
445 * the parent-symlink-race gap that otherwise forces the path-based set. */
446 avail = proc_self_fd_usable();
447 }
448 return avail;
449 }
450
451 #endif /* SUPPORT_ACL_FD */