Skip to content

Commit

Permalink
apparmor: prefer /proc/.../attr/apparmor/current over legacy interface
Browse files Browse the repository at this point in the history
It turns out that since Linux 5.1 there are now per-LSM subdirectories
for major LSMs, which users are recommended to use over the "legacy"
top-level /proc/$pid/attr/... files[1]:

> Process attributes associated with “major” security modules should be
> accessed and maintained using the special files in /proc/.../attr. A
> security module may maintain a module specific subdirectory there,
> named after the module. /proc/.../attr/smack is provided by the Smack
> security module and contains all its special files. The files directly
> in /proc/.../attr remain as legacy interfaces for modules that provide
> subdirectories.

AppArmor has had such a directory since Linux 5.8[2], and it turns out
that with certain CONFIG_LSM configurations you can end up with AppArmor
files not being accessible from the legacy interface. Arch Linux
recently added BPF as one of the enabled LSM in their configuration, and
this broke runc[3] and LXC.

The solution is to first try to use /proc/$pid/attr/apparmor/current and
fall back to /proc/$pid/attr/current if the former is not available.

[1]: https://www.kernel.org/doc/html/latest/admin-guide/LSM/index.html
[2]: Linux 5.8 ; commit 6413f852ce08 ("apparmor: add proc subdir to attrs")
[3]: opencontainers/runc#2801

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Feb 19, 2021
1 parent 301a5f8 commit 47f4914
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/lxc/lsm/apparmor.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,28 @@ static int __apparmor_process_label_open(struct lsm_ops *ops, pid_t pid, int o_f
if (on_exec)
TRACE("On-exec not supported with AppArmor");

/* first try the apparmor subdir */
ret = snprintf(path, LXC_LSMATTRLEN, "/proc/%d/attr/apparmor/current", pid);
if (ret < 0 || ret >= LXC_LSMATTRLEN)
return -1;

labelfd = open(path, o_flags);
if (labelfd >= 0)
return labelfd;
else if (errno != ENOENT)
goto error;

/* fallback to legacy global attr directory */
ret = snprintf(path, LXC_LSMATTRLEN, "/proc/%d/attr/current", pid);
if (ret < 0 || ret >= LXC_LSMATTRLEN)
return -1;

labelfd = open(path, o_flags);
if (labelfd < 0)
return log_error_errno(-errno, errno, "Unable to open AppArmor LSM label file descriptor");
if (labelfd >= 0)
return labelfd;

return labelfd;
error:
return log_error_errno(-errno, errno, "Unable to open AppArmor LSM label file descriptor");
}

static char *apparmor_process_label_get(struct lsm_ops *ops, pid_t pid)
Expand All @@ -439,14 +452,16 @@ static char *apparmor_process_label_get_at(struct lsm_ops *ops, int fd_pid)
__do_free char *label = NULL;
size_t len;

label = read_file_at(fd_pid, "attr/current", PROTECT_OPEN, PROTECT_LOOKUP_BENEATH);
/* first try the apparmor subdir, then fall back to legacy interface */
label = read_file_at(fd_pid, "attr/apparmor/current", PROTECT_OPEN, PROTECT_LOOKUP_BENEATH);
if (!label && errno == ENOENT)
label = read_file_at(fd_pid, "attr/current", PROTECT_OPEN, PROTECT_LOOKUP_BENEATH);
if (!label)
return log_error_errno(NULL, errno, "Failed to get AppArmor context");

len = strcspn(label, "\n \t");
if (len)
label[len] = '\0';

return move_ptr(label);
}

Expand Down
4 changes: 3 additions & 1 deletion src/lxc/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,13 @@
* +
* /attr/ = 6
* +
* /apparmor/ = 10
* +
* /current = 8
* +
* \0 = 1
*/
#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 10 + 8 + 1)

/* MAX_NS_PROC_NAME = MAX_NS_PROC_NAME
* +
Expand Down

0 comments on commit 47f4914

Please sign in to comment.