tmpfiles: add the a and A entries, POSIX ACLs

The tpm2-tss package sets a default ACL on its keystore:

    d  /var/lib/tpm2-tss/system/keystore 2775 tss tss - -
    a+ /var/lib/tpm2-tss/system/keystore -    -   -   - default:group:tss:rwx

tmpfiles skipped the a+ line as unsupported, so what was created in
the keystore did not get the group access it relies on.

Add a and A, the latter recursive, with and without +.  With + the
entries are added to the ACL of the path, without it they replace it.
The ACL is written to the system.posix_acl_access and _default
extended attributes in the format the kernel uses, which saves a
dependency on libacl.  As with setfacl, the mask is made to cover the
named entries unless the entry sets one.

An ACL can have at most 32 entries.  A path with more is left as it
is, with a warning.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:02:33 +01:00
1 parent 3bc7c83731
commit f8571aba1f
4 files changed
+406

No files matched your search

+325
View File
@@ -24,6 +24,7 @@
#include "config.h" /* Generated by configure script */
#include <dirent.h>
#include <endian.h>
#include <fcntl.h>
#include <ftw.h>
#include <getopt.h>
@@ -36,6 +37,10 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
#ifdef _LIBITE_LITE
# include <libite/lite.h>
#else
@@ -376,6 +381,320 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
return 0;
}
/*
* POSIX ACLs for the a and A entries. Set straight
* 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 {
int tag;
unsigned id;
int perm;
};
struct acl {
struct ace e[ACL_MAX];
int num;
};
struct acl_xattr {
struct posix_acl_xattr_header hdr;
struct posix_acl_xattr_entry e[ACL_MAX];
};
static struct ace *acl_find(struct acl *acl, int tag, unsigned id)
{
for (int i = 0; i < acl->num; i++) {
if (acl->e[i].tag == tag && acl->e[i].id == id)
return &acl->e[i];
}
return NULL;
}
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;
return -1;
}
e = &acl->e[acl->num++];
e->tag = tag;
e->id = id;
}
e->perm = perm;
return 0;
}
static int acl_parse_perm(const char *str)
{
int perm = 0;
if (str[0] >= '0' && str[0] <= '7' && !str[1])
return str[0] - '0';
for (; *str; str++) {
switch (*str) {
case 'r': perm |= ACL_READ; break;
case 'w': perm |= ACL_WRITE; break;
case 'x': perm |= ACL_EXECUTE; break;
case 'X': perm |= ACL_COND_EXEC; break;
case '-': break;
default:
return -1;
}
}
return perm;
}
/* [d[efault]:]u[ser]|g[roup]|m[ask]|o[ther]:[NAME|ID]:PERMS */
static int acl_parse_entry(char *str, struct acl *access, struct acl *def)
{
struct acl *acl = access;
char *tag, *name, *perms;
unsigned id = ACL_UNDEFINED_ID;
int perm, type;
long val;
tag = strsep(&str, ":");
if (!strcmp(tag, "d") || !strcmp(tag, "default")) {
acl = def;
tag = strsep(&str, ":");
}
name = strsep(&str, ":");
perms = strsep(&str, ":");
if (!perms) { /* m:rwx and o:rx, without the empty name */
perms = name;
name = "";
}
if (!tag || !perms || str)
return -1;
perm = acl_parse_perm(perms);
if (perm < 0)
return -1;
if (!strcmp(tag, "u") || !strcmp(tag, "user"))
type = name[0] ? ACL_USER : ACL_USER_OBJ;
else if (!strcmp(tag, "g") || !strcmp(tag, "group"))
type = name[0] ? ACL_GROUP : ACL_GROUP_OBJ;
else if (!strcmp(tag, "m") || !strcmp(tag, "mask"))
type = ACL_MASK;
else if (!strcmp(tag, "o") || !strcmp(tag, "other"))
type = ACL_OTHER;
else
return -1;
if (name[0]) {
if (type != ACL_USER && type != ACL_GROUP)
return -1;
if (type == ACL_USER)
val = parse_uid(name);
else if ((val = atonum(name)) < 0)
val = getgroup(name);
if (val < 0)
return -1;
id = val;
}
return acl_set(acl, type, id, perm);
}
static int acl_parse(char *arg, struct acl *access, struct acl *def)
{
char *entry;
while ((entry = strsep(&arg, ", \t"))) {
if (!entry[0])
continue;
if (acl_parse_entry(entry, access, def))
return -1;
}
return 0;
}
/* 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;
ssize_t len;
len = getxattr(path, name, &buf, sizeof(buf));
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;
return -1;
}
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);
}
return 0;
}
static int acl_cmp(const void *a, const void *b)
{
const struct ace *x = a, *y = b;
if (x->tag != y->tag)
return x->tag - y->tag;
return (x->id > y->id) - (x->id < y->id);
}
static int acl_write(const char *path, const char *name, struct acl *acl)
{
struct acl_xattr buf;
qsort(acl->e, acl->num, sizeof(acl->e[0]), acl_cmp);
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);
}
return setxattr(path, name, &buf, sizeof(buf.hdr) + acl->num * sizeof(buf.e[0]), 0);
}
/*
* Set the entries in @new on @path, on top of the ACL it has when
* @modify is set. What a complete ACL needs and @new lacks comes from
* the ACL it has, or else the mode in @st, and the mask is made to
* cover every named entry.
*/
static int acl_apply(const char *path, const char *name, const struct acl *new, int modify,
const struct stat *st)
{
const int base[][2] = {
{ ACL_USER_OBJ, (st->st_mode >> 6) & 7 },
{ ACL_GROUP_OBJ, (st->st_mode >> 3) & 7 },
{ ACL_OTHER, st->st_mode & 7 },
};
struct acl acl = { 0 }, old = { 0 };
int mask = 0, named = 0, has_mask = 0;
if (acl_read(path, name, &old))
return -1;
if (modify)
acl = old;
for (int i = 0; i < new->num; i++) {
const struct ace *e = &new->e[i];
int perm = e->perm & 7;
if ((e->perm & ACL_COND_EXEC) && (S_ISDIR(st->st_mode) || (st->st_mode & 0111)))
perm |= ACL_EXECUTE;
if (e->tag == ACL_MASK)
has_mask = 1;
if (acl_set(&acl, e->tag, e->id, perm))
return -1;
}
for (size_t i = 0; i < NELEMS(base); i++) {
struct ace *e = acl_find(&old, 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]))
return -1;
}
for (int i = 0; i < acl.num; i++) {
if (acl.e[i].tag == ACL_USER || acl.e[i].tag == ACL_GROUP)
named = 1;
if (acl.e[i].tag & (ACL_USER | ACL_GROUP_OBJ | ACL_GROUP))
mask |= acl.e[i].perm;
}
if (named && !has_mask && acl_set(&acl, ACL_MASK, ACL_UNDEFINED_ID, mask))
return -1;
return acl_write(path, name, &acl);
}
/* Globals for acl_cb(), nftw() cannot pass user data */
static const struct acl *acl_access, *acl_default;
static int acl_modify;
static int acl_path(const char *path)
{
char proc[32];
struct stat st;
int fd, rc = 0;
/* Through /proc so a link at @path is never followed */
fd = open(path, O_PATH | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0)
return -1;
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
if (fstat(fd, &st)) {
rc = -1;
} else if (!S_ISLNK(st.st_mode)) {
if (acl_access->num)
rc = acl_apply(proc, "system.posix_acl_access", acl_access, acl_modify, &st);
if (!rc && acl_default->num && S_ISDIR(st.st_mode))
rc = acl_apply(proc, "system.posix_acl_default", acl_default, acl_modify, &st);
}
close(fd);
return rc;
}
static int acl_cb(const char *path, const struct stat *st, int tflag, struct FTW *ftw)
{
(void)st; (void)tflag; (void)ftw;
if (acl_path(path))
warn("Failed setting ACL on %s", path);
return 0;
}
static int acl_entry(const char *pattern, char *arg, int recursive, int modify)
{
struct acl access = { 0 }, def = { 0 };
glob_t gl;
int rc = 0;
if (!arg || acl_parse(arg, &access, &def)) {
errno = EINVAL;
return -1;
}
if (glob(pattern, GLOB_NOESCAPE, NULL, &gl))
return 0;
acl_access = &access;
acl_default = &def;
acl_modify = modify;
for (size_t i = 0; i < gl.gl_pathc; i++) {
if (recursive)
nftw(gl.gl_pathv[i], acl_cb, 20, FTW_PHYS);
else
rc |= acl_path(gl.gl_pathv[i]);
}
globfree(&gl);
return rc;
}
/*
* The configuration format is one line per path, containing type, path,
* mode, ownership, age, and argument fields. The lines are separated by
@@ -441,6 +760,8 @@ static void tmpfiles(char *line)
// file and directory removal logic
if (remove_flag) {
switch (type[0]) {
case 'a':
case 'A':
case 'b':
case 'c':
case 'C':
@@ -508,6 +829,10 @@ static void tmpfiles(char *line)
// file & directory creation logic
if (create_flag) {
switch (type[0]) {
case 'a':
case 'A':
rc = acl_entry(path, arg, type[0] == 'A', type[1] == '+');
break;
case 'b':
rc = parse_mm(arg, &major, &minor);
if (rc)