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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 19:23:47 +01:00
1 parent c7f55a8b90
commit ce5a08e866
3 files changed
+79 -34

No files matched your search

-2
View File
@@ -32,5 +32,3 @@ Missing options: `--root`, `--replace`, and `--purge`.
would be the one in `/usr/lib`. Both changes have to go in together 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 * A relative path is taken as it is, tmpfiles.d(5) only has absolute
ones ones
* `a` and `A` handle an ACL of at most 32 entries, a path with more is
left as it is, with a warning
+67 -32
View File
@@ -879,7 +879,6 @@ static int clean_dir(const char *path)
* through the system.posix_acl_* extended attributes, in the kernel's * through the system.posix_acl_* extended attributes, in the kernel's
* own format, so there is no need for libacl. * 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 */ #define ACL_COND_EXEC 0x100 /* X, execute only for a directory or an executable */
struct ace { struct ace {
@@ -889,13 +888,13 @@ struct ace {
}; };
struct acl { struct acl {
struct ace e[ACL_MAX]; struct ace *e;
int num; int num;
}; };
struct acl_xattr { struct acl_xattr {
struct posix_acl_xattr_header hdr; 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) 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); struct ace *e = acl_find(acl, tag, id);
if (!e) { if (!e) {
if (acl->num == ACL_MAX) { e = reallocarray(acl->e, acl->num + 1, sizeof(*e));
errno = ENOSPC; if (!e)
return -1; return -1;
} acl->e = e;
e = &acl->e[acl->num++]; e = &acl->e[acl->num++];
e->tag = tag; e->tag = tag;
e->id = id; 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 */ /* 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) static int acl_read(const char *path, const char *name, struct acl *acl)
{ {
struct acl_xattr buf; struct acl_xattr *buf;
ssize_t len; ssize_t len;
int rc = -1;
len = getxattr(path, name, &buf, sizeof(buf)); len = getxattr(path, name, NULL, 0);
if (len < 0) if (len < 0)
return errno == ENODATA ? 0 : -1; 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; 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]); len = (len - sizeof(buf->hdr)) / sizeof(buf->e[0]);
for (int i = 0; i < acl->num; i++) { for (ssize_t i = 0; i < len; i++) {
acl->e[i].tag = le16toh(buf.e[i].e_tag); if (acl_set(acl, le16toh(buf->e[i].e_tag), le32toh(buf->e[i].e_id), le16toh(buf->e[i].e_perm)))
acl->e[i].perm = le16toh(buf.e[i].e_perm); goto done;
acl->e[i].id = le32toh(buf.e[i].e_id);
} }
rc = 0;
done:
free(buf);
return 0; return rc;
} }
static int acl_cmp(const void *a, const void *b) 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) 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); 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++) { for (int i = 0; i < acl->num; i++) {
buf.e[i].e_tag = htole16(acl->e[i].tag); buf->e[i].e_tag = htole16(acl->e[i].tag);
buf.e[i].e_perm = htole16(acl->e[i].perm); buf->e[i].e_perm = htole16(acl->e[i].perm);
buf.e[i].e_id = htole32(acl->e[i].id); 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 }; struct acl acl = { 0 }, old = { 0 };
int mask = 0, named = 0, has_mask = 0; int mask = 0, named = 0, has_mask = 0;
int rc = -1;
if (acl_read(path, name, &old)) if (acl_read(path, name, &old))
return -1; goto done;
if (modify)
acl = old; 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++) { for (int i = 0; i < new->num; i++) {
const struct ace *e = &new->e[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) if (e->tag == ACL_MASK)
has_mask = 1; has_mask = 1;
if (acl_set(&acl, e->tag, e->id, perm)) if (acl_set(&acl, e->tag, e->id, perm))
return -1; goto done;
} }
for (size_t i = 0; i < NELEMS(base); i++) { 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) && 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])) 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++) { 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; mask |= acl.e[i].perm;
} }
if (named && !has_mask && acl_set(&acl, ACL_MASK, ACL_UNDEFINED_ID, mask)) 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 */ /* 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)) { if (!arg || acl_parse(arg, &access, &def)) {
errno = EINVAL; errno = EINVAL;
return -1; rc = -1;
goto done;
} }
if (glob(pattern, GLOB_NOESCAPE, NULL, &gl)) if (glob(pattern, GLOB_NOESCAPE, NULL, &gl))
return 0; goto done;
acl_access = &access; acl_access = &access;
acl_default = &def; 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]); rc |= acl_path(gl.gl_pathv[i]);
} }
globfree(&gl); globfree(&gl);
done:
free(access.e);
free(def.e);
return rc; return rc;
} }
+12
View File
@@ -63,6 +63,18 @@ a+ $DIR/file - - - - m::r
CONF CONF
assert_mode $DIR/file 640 assert_mode $DIR/file 640
sep
say 'An ACL can have many entries'
run "touch $DIR/many; chmod 600 $DIR/many"
tmpfiles <<CONF
a $DIR/many - - - - $(seq -s, -f 'u:%g:r' 2000 2039)
CONF
assert_mode $DIR/many 640
tmpfiles <<CONF
a+ $DIR/many - - - - u:2040:-w-
CONF
assert_mode $DIR/many 660
sep sep
say 'A is recursive, and X is x only for a directory or an executable' say 'A is recursive, and X is x only for a directory or an executable'
run "mkdir -p $DIR/tree/sub; touch $DIR/tree/sub/file $DIR/tree/sub/prog" run "mkdir -p $DIR/tree/sub; touch $DIR/tree/sub/file $DIR/tree/sub/prog"