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

+3
View File
@@ -11,6 +11,9 @@ All relevant changes are documented in this file.
- `shutdown` now takes a time, `now`, `+MIN`, or `HH:MM`, and a message.
Finit warns logged-in users as the time approaches, refuses new logins
from five minutes before, and `shutdown -c` cancels. Issue #149
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
by for example the tpm2-tss package. Finit sets them itself, no need
for libacl or `setfacl`
- The `restart_sec:` option of the line-based format is now spelled
`restart-sec:`, kebab-case like every other setting. The old spelling
is still accepted
+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)
+2
View File
@@ -81,6 +81,7 @@ EXTRA_DIST += start-stop-sysv.sh
EXTRA_DIST += start-stop-serv.sh
EXTRA_DIST += signal-service.sh
EXTRA_DIST += testserv.sh
EXTRA_DIST += tmpfiles-acl.sh
EXTRA_DIST += tmpfiles-arg.sh
EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += unexpected-restart.sh
@@ -160,6 +161,7 @@ TESTS += start-stop-serv.sh
TESTS += signal-service.sh
if TESTSERV
TESTS += testserv.sh
TESTS += tmpfiles-acl.sh
TESTS += tmpfiles-arg.sh
TESTS += tmpfiles-perm.sh
endif
+76
View File
@@ -0,0 +1,76 @@
#!/bin/sh
# Verify the a and A entries of tmpfiles, POSIX ACLs. The sysroot has
# no getfacl, so this looks at what an ACL does to the mode: the group
# bits show the mask, and a default ACL replaces the umask for what is
# created in the directory.
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/acl
CONF=/tmp/acl.conf
tmpfiles()
{
run "cat > $CONF"
run "/libexec/finit/tmpfiles --create $CONF"
}
assert_mode()
{
assert "Mode of $1 is $2" "$(texec stat -c %a "$1")" = "$2"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
sep
say 'Default ACL on a directory, as in tpm2-tss-fapi.conf'
tmpfiles <<CONF
#Type Path Mode User Group Age Argument
d $DIR/keystore 2775 daemon daemon - -
a+ $DIR/keystore - - - - default:group:daemon:rwx
d $DIR/plain 2775 daemon daemon - -
CONF
run "umask 077; mkdir $DIR/keystore/sub $DIR/plain/sub; touch $DIR/keystore/file"
assert_mode $DIR/plain/sub 2700
assert_mode $DIR/keystore/sub 2775
assert_mode $DIR/keystore/file 664
sep
say 'a+ adds to the ACL of a file, a replaces it'
run "touch $DIR/file; chmod 600 $DIR/file"
tmpfiles <<CONF
a+ $DIR/file - - - - user:daemon:r--
CONF
assert_mode $DIR/file 640
tmpfiles <<CONF
a+ $DIR/file - - - - g:bin:rw-
CONF
assert_mode $DIR/file 660
tmpfiles <<CONF
a $DIR/file - - - - u:sys:--x
CONF
assert_mode $DIR/file 610
tmpfiles <<CONF
a+ $DIR/file - - - - m::r
CONF
assert_mode $DIR/file 640
sep
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 "chmod 700 $DIR/tree $DIR/tree/sub $DIR/tree/sub/prog; chmod 600 $DIR/tree/sub/file"
tmpfiles <<CONF
A $DIR/tree - - - - u:daemon:rwX
CONF
assert_mode $DIR/tree 770
assert_mode $DIR/tree/sub 770
assert_mode $DIR/tree/sub/prog 770
assert_mode $DIR/tree/sub/file 660