From ce5a08e866f0b45ab3f7da287f932d37d1783459 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 18:58:37 +0100 Subject: [PATCH] tmpfiles: handle an ACL of any size An a or A entry for a path with an ACL of more than 32 entries, or one that would get more, left the path as it was with a warning. The ACL was kept in a fixed array. It is now allocated to the size it needs. Signed-off-by: Joachim Wiberg --- doc/tmpfiles-todo.md | 2 - src/tmpfiles.c | 99 ++++++++++++++++++++++++++++++-------------- test/tmpfiles-acl.sh | 12 ++++++ 3 files changed, 79 insertions(+), 34 deletions(-) diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index f873ebcf..0f08f2a7 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -32,5 +32,3 @@ Missing options: `--root`, `--replace`, and `--purge`. would be the one in `/usr/lib`. Both changes have to go in together * A relative path is taken as it is, tmpfiles.d(5) only has absolute ones -* `a` and `A` handle an ACL of at most 32 entries, a path with more is - left as it is, with a warning diff --git a/src/tmpfiles.c b/src/tmpfiles.c index eec16b35..855d24aa 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -879,7 +879,6 @@ static int clean_dir(const char *path) * through the system.posix_acl_* extended attributes, in the kernel's * own format, so there is no need for libacl. */ -#define ACL_MAX 32 #define ACL_COND_EXEC 0x100 /* X, execute only for a directory or an executable */ struct ace { @@ -889,13 +888,13 @@ struct ace { }; struct acl { - struct ace e[ACL_MAX]; - int num; + struct ace *e; + int num; }; struct acl_xattr { struct posix_acl_xattr_header hdr; - struct posix_acl_xattr_entry e[ACL_MAX]; + struct posix_acl_xattr_entry e[]; }; static struct ace *acl_find(struct acl *acl, int tag, unsigned id) @@ -913,10 +912,10 @@ static int acl_set(struct acl *acl, int tag, unsigned id, int perm) struct ace *e = acl_find(acl, tag, id); if (!e) { - if (acl->num == ACL_MAX) { - errno = ENOSPC; + e = reallocarray(acl->e, acl->num + 1, sizeof(*e)); + if (!e) return -1; - } + acl->e = e; e = &acl->e[acl->num++]; e->tag = tag; e->id = id; @@ -1019,25 +1018,39 @@ static int acl_parse(char *arg, struct acl *access, struct acl *def) /* No ACL is fine, one that cannot be read must not be replaced */ static int acl_read(const char *path, const char *name, struct acl *acl) { - struct acl_xattr buf; + struct acl_xattr *buf; ssize_t len; + int rc = -1; - len = getxattr(path, name, &buf, sizeof(buf)); + len = getxattr(path, name, NULL, 0); if (len < 0) return errno == ENODATA ? 0 : -1; - if (len < (ssize_t)sizeof(buf.hdr) || le32toh(buf.hdr.a_version) != POSIX_ACL_XATTR_VERSION) { - errno = EINVAL; + + buf = malloc(len + sizeof(*buf)); + if (!buf) return -1; + + len = getxattr(path, name, buf, len); + if (len < 0) { + if (errno == ENODATA) + rc = 0; + goto done; + } + if (len < (ssize_t)sizeof(buf->hdr) || le32toh(buf->hdr.a_version) != POSIX_ACL_XATTR_VERSION) { + errno = EINVAL; + goto done; } - acl->num = (len - sizeof(buf.hdr)) / sizeof(buf.e[0]); - for (int i = 0; i < acl->num; i++) { - acl->e[i].tag = le16toh(buf.e[i].e_tag); - acl->e[i].perm = le16toh(buf.e[i].e_perm); - acl->e[i].id = le32toh(buf.e[i].e_id); + len = (len - sizeof(buf->hdr)) / sizeof(buf->e[0]); + for (ssize_t i = 0; i < len; i++) { + if (acl_set(acl, le16toh(buf->e[i].e_tag), le32toh(buf->e[i].e_id), le16toh(buf->e[i].e_perm))) + goto done; } + rc = 0; +done: + free(buf); - return 0; + return rc; } static int acl_cmp(const void *a, const void *b) @@ -1052,18 +1065,27 @@ static int acl_cmp(const void *a, const void *b) static int acl_write(const char *path, const char *name, struct acl *acl) { - struct acl_xattr buf; + size_t len = sizeof(struct acl_xattr) + acl->num * sizeof(struct posix_acl_xattr_entry); + struct acl_xattr *buf; + int rc; + + buf = malloc(len); + if (!buf) + return -1; qsort(acl->e, acl->num, sizeof(acl->e[0]), acl_cmp); - buf.hdr.a_version = htole32(POSIX_ACL_XATTR_VERSION); + buf->hdr.a_version = htole32(POSIX_ACL_XATTR_VERSION); for (int i = 0; i < acl->num; i++) { - buf.e[i].e_tag = htole16(acl->e[i].tag); - buf.e[i].e_perm = htole16(acl->e[i].perm); - buf.e[i].e_id = htole32(acl->e[i].id); + buf->e[i].e_tag = htole16(acl->e[i].tag); + buf->e[i].e_perm = htole16(acl->e[i].perm); + buf->e[i].e_id = htole32(acl->e[i].id); } - return setxattr(path, name, &buf, sizeof(buf.hdr) + acl->num * sizeof(buf.e[0]), 0); + rc = setxattr(path, name, buf, len, 0); + free(buf); + + return rc; } /* @@ -1082,11 +1104,15 @@ static int acl_apply(const char *path, const char *name, const struct acl *new, }; struct acl acl = { 0 }, old = { 0 }; int mask = 0, named = 0, has_mask = 0; + int rc = -1; if (acl_read(path, name, &old)) - return -1; - if (modify) - acl = old; + goto done; + + for (int i = 0; modify && i < old.num; i++) { + if (acl_set(&acl, old.e[i].tag, old.e[i].id, old.e[i].perm)) + goto done; + } for (int i = 0; i < new->num; i++) { const struct ace *e = &new->e[i]; @@ -1097,7 +1123,7 @@ static int acl_apply(const char *path, const char *name, const struct acl *new, if (e->tag == ACL_MASK) has_mask = 1; if (acl_set(&acl, e->tag, e->id, perm)) - return -1; + goto done; } for (size_t i = 0; i < NELEMS(base); i++) { @@ -1105,7 +1131,7 @@ static int acl_apply(const char *path, const char *name, const struct acl *new, if (!acl_find(&acl, base[i][0], ACL_UNDEFINED_ID) && acl_set(&acl, base[i][0], ACL_UNDEFINED_ID, e ? e->perm : base[i][1])) - return -1; + goto done; } for (int i = 0; i < acl.num; i++) { @@ -1115,9 +1141,14 @@ static int acl_apply(const char *path, const char *name, const struct acl *new, mask |= acl.e[i].perm; } if (named && !has_mask && acl_set(&acl, ACL_MASK, ACL_UNDEFINED_ID, mask)) - return -1; + goto done; - return acl_write(path, name, &acl); + rc = acl_write(path, name, &acl); +done: + free(acl.e); + free(old.e); + + return rc; } /* Globals for acl_cb(), nftw() cannot pass user data */ @@ -1167,11 +1198,12 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify) if (!arg || acl_parse(arg, &access, &def)) { errno = EINVAL; - return -1; + rc = -1; + goto done; } if (glob(pattern, GLOB_NOESCAPE, NULL, &gl)) - return 0; + goto done; acl_access = &access; acl_default = &def; @@ -1184,6 +1216,9 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify) rc |= acl_path(gl.gl_pathv[i]); } globfree(&gl); +done: + free(access.e); + free(def.e); return rc; } diff --git a/test/tmpfiles-acl.sh b/test/tmpfiles-acl.sh index 1f4bfafa..754e7440 100755 --- a/test/tmpfiles-acl.sh +++ b/test/tmpfiles-acl.sh @@ -63,6 +63,18 @@ a+ $DIR/file - - - - m::r CONF assert_mode $DIR/file 640 +sep +say 'An ACL can have many entries' +run "touch $DIR/many; chmod 600 $DIR/many" +tmpfiles <