2010-12-20 12:29:10 -08:00
|
|
|
/*
|
2011-02-22 03:51:16 -08:00
|
|
|
* Copyright (c) 2003-2008 Novell, Inc. (All rights reserved)
|
|
|
|
* Copyright 2009-2010 Canonical Ltd.
|
|
|
|
*
|
|
|
|
* The libapparmor library is licensed under the terms of the GNU
|
|
|
|
* Lesser General Public License, version 2.1. Please see the file
|
|
|
|
* COPYING.LGPL.
|
|
|
|
*
|
2011-02-23 14:02:45 -08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
2011-02-22 03:51:16 -08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2007-07-28 15:41:04 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/syscall.h>
|
2011-08-09 06:47:40 -07:00
|
|
|
#include <sys/socket.h>
|
2007-07-28 15:41:04 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2010-02-11 15:38:24 -08:00
|
|
|
#include <stdarg.h>
|
2011-08-09 06:48:17 -07:00
|
|
|
#include <mntent.h>
|
2013-07-31 09:22:40 -07:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <pthread.h>
|
2007-07-28 15:41:04 +00:00
|
|
|
|
2014-01-06 14:08:55 -08:00
|
|
|
#include <sys/apparmor.h>
|
2015-06-06 01:26:03 -07:00
|
|
|
#include "private.h"
|
2011-08-09 06:48:56 -07:00
|
|
|
|
2011-05-27 14:20:03 -07:00
|
|
|
/* some non-Linux systems do not define a static value */
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
2007-08-16 04:19:54 +00:00
|
|
|
#define symbol_version(real, name, version) \
|
|
|
|
__asm__ (".symver " #real "," #name "@" #version)
|
|
|
|
#define default_symbol_version(real, name, version) \
|
|
|
|
__asm__ (".symver " #real "," #name "@@" #version)
|
|
|
|
|
2015-05-19 21:20:37 -05:00
|
|
|
#define UNCONFINED "unconfined"
|
|
|
|
#define UNCONFINED_SIZE strlen(UNCONFINED)
|
|
|
|
|
2011-08-09 06:48:17 -07:00
|
|
|
/**
|
|
|
|
* aa_find_mountpoint - find where the apparmor interface filesystem is mounted
|
|
|
|
* @mnt: returns buffer with the mountpoint string
|
|
|
|
*
|
|
|
|
* Returns: 0 on success else -1 on error
|
|
|
|
*
|
|
|
|
* NOTE: this function only supports versions of apparmor using securityfs
|
|
|
|
*/
|
|
|
|
int aa_find_mountpoint(char **mnt)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
struct mntent *mntpt;
|
|
|
|
FILE *mntfile;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
if (!mnt) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mntfile = setmntent("/proc/mounts", "r");
|
|
|
|
if (!mntfile)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((mntpt = getmntent(mntfile))) {
|
|
|
|
char *proposed = NULL;
|
|
|
|
if (strcmp(mntpt->mnt_type, "securityfs") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (asprintf(&proposed, "%s/apparmor", mntpt->mnt_dir) < 0)
|
|
|
|
/* ENOMEM */
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (stat(proposed, &statbuf) == 0) {
|
|
|
|
*mnt = proposed;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(proposed);
|
|
|
|
}
|
|
|
|
endmntent(mntfile);
|
|
|
|
if (rc == -1)
|
|
|
|
errno = ENOENT;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-04-27 05:21:58 -07:00
|
|
|
// done as a macro so we can paste the param
|
|
|
|
|
|
|
|
#define param_check_base(PARAM) \
|
|
|
|
({ \
|
|
|
|
int rc, fd; \
|
|
|
|
fd = open("/sys/module/apparmor/parameters/" PARAM, O_RDONLY); \
|
|
|
|
if (fd == -1) { \
|
|
|
|
rc = errno; \
|
|
|
|
} else { \
|
|
|
|
char buffer[2]; \
|
|
|
|
int size = read(fd, &buffer, 2); \
|
|
|
|
rc = errno; \
|
|
|
|
close(fd); \
|
|
|
|
errno = rc; \
|
|
|
|
if (size > 0) { \
|
|
|
|
if (buffer[0] == 'Y') \
|
|
|
|
rc = 0; \
|
|
|
|
else \
|
|
|
|
rc = ECANCELED; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
(rc); \
|
|
|
|
})
|
|
|
|
|
|
|
|
static pthread_once_t param_enabled_ctl = PTHREAD_ONCE_INIT;
|
|
|
|
static int param_enabled = 0;
|
|
|
|
|
|
|
|
static pthread_once_t param_private_enabled_ctl = PTHREAD_ONCE_INIT;
|
|
|
|
static int param_private_enabled = 0;
|
|
|
|
|
|
|
|
static void param_check_enabled_init_once(void)
|
|
|
|
{
|
|
|
|
param_enabled = param_check_base("enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int param_check_enabled()
|
|
|
|
{
|
|
|
|
if (pthread_once(¶m_enabled_ctl, param_check_enabled_init_once) == 0)
|
|
|
|
return param_enabled;
|
|
|
|
return param_check_base("enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_enabled(void)
|
|
|
|
{
|
|
|
|
return !param_check_enabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void param_check_private_enabled_init_once(void)
|
|
|
|
{
|
|
|
|
param_enabled = param_check_base("private_enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int param_check_private_enabled()
|
|
|
|
{
|
|
|
|
if (pthread_once(¶m_private_enabled_ctl, param_check_private_enabled_init_once) == 0)
|
|
|
|
return param_private_enabled;
|
|
|
|
return param_check_base("private_enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_private_enabled(void)
|
|
|
|
{
|
|
|
|
return !param_check_private_enabled();
|
|
|
|
}
|
|
|
|
|
2011-08-09 06:48:56 -07:00
|
|
|
/**
|
|
|
|
* aa_is_enabled - determine if apparmor is enabled
|
|
|
|
*
|
|
|
|
* Returns: 1 if enabled else reason it is not, or 0 on error
|
|
|
|
*
|
|
|
|
* ENOSYS - no indication apparmor is present in the system
|
|
|
|
* ENOENT - enabled but interface could not be found
|
|
|
|
* ECANCELED - disabled at boot
|
|
|
|
* ENOMEM - out of memory
|
|
|
|
*/
|
|
|
|
int aa_is_enabled(void)
|
|
|
|
{
|
2020-04-27 05:21:58 -07:00
|
|
|
int rc;
|
2011-08-09 06:48:56 -07:00
|
|
|
char *mnt;
|
2020-04-27 05:21:58 -07:00
|
|
|
bool private = false;
|
2011-08-09 06:48:56 -07:00
|
|
|
|
2020-04-27 05:21:58 -07:00
|
|
|
rc = param_check_enabled();
|
|
|
|
if (rc) {
|
|
|
|
if (rc == ENOENT)
|
|
|
|
errno = ENOSYS;
|
|
|
|
else
|
|
|
|
errno = rc;
|
|
|
|
|
|
|
|
if (!is_private_enabled())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* actually available but only on private interfaces */
|
|
|
|
private = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the interface mountpoint is available apparmor may not
|
|
|
|
* be locally enabled for older interfaces but still present
|
|
|
|
* so make sure to check after, checking available status
|
|
|
|
* also we don't cache the enabled status like available
|
|
|
|
* because the mount status can change.
|
|
|
|
*/
|
2011-08-09 06:48:56 -07:00
|
|
|
rc = aa_find_mountpoint(&mnt);
|
|
|
|
if (rc == 0) {
|
|
|
|
free(mnt);
|
2020-04-27 05:21:58 -07:00
|
|
|
if (!private)
|
|
|
|
return 1;
|
|
|
|
/* provide an error code to indicate apparmor is available
|
|
|
|
* on private interfaces, but we can note that apparmor
|
|
|
|
* is enabled because some applications hit the low level
|
|
|
|
* interfaces directly and don't know about the new
|
|
|
|
* private interfaces
|
|
|
|
*/
|
|
|
|
errno = EBUSY;
|
|
|
|
/* fall through to return 0 */
|
2011-08-09 06:48:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-09 06:48:17 -07:00
|
|
|
|
2011-07-21 11:06:57 -07:00
|
|
|
static inline pid_t aa_gettid(void)
|
|
|
|
{
|
|
|
|
#ifdef SYS_gettid
|
|
|
|
return syscall(SYS_gettid);
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:06:38 -07:00
|
|
|
/*
|
|
|
|
* Check for the new apparmor proc interface once on the first api call
|
|
|
|
* and then reuse the result on all subsequent api calls. This avoids
|
|
|
|
* a double syscall overhead on each api call if the interface is not
|
|
|
|
* present.
|
|
|
|
*/
|
|
|
|
static pthread_once_t proc_attr_base_ctl = PTHREAD_ONCE_INIT;
|
2020-11-02 17:16:55 -08:00
|
|
|
static const char *proc_attr_base_old = "/proc/%d/attr/%s";
|
|
|
|
static const char *proc_attr_base_stacking = "/proc/%d/attr/apparmor/%s";
|
|
|
|
static const char *proc_attr_base_unavailable = "/proc/%d/attr/apparmor/unavailable/%s";
|
|
|
|
static const char *proc_attr_base;
|
2020-04-26 14:06:38 -07:00
|
|
|
|
|
|
|
static void proc_attr_base_init_once(void)
|
|
|
|
{
|
|
|
|
autofree char *tmp;
|
|
|
|
|
|
|
|
/* if we fail we just fall back to the default value */
|
|
|
|
if (asprintf(&tmp, "/proc/%d/attr/apparmor/current", aa_gettid())) {
|
|
|
|
autoclose int fd = open(tmp, O_RDONLY);
|
|
|
|
if (fd != -1)
|
|
|
|
proc_attr_base = proc_attr_base_stacking;
|
2020-04-27 05:21:58 -07:00
|
|
|
} else if (!is_enabled() && is_private_enabled()) {
|
|
|
|
/* new stacking interfaces aren't available and apparmor
|
|
|
|
* is disabled, but available. do not use the
|
|
|
|
* /proc/<pid>/attr/ * interfaces as they could be
|
|
|
|
* in use by another LSM
|
|
|
|
*/
|
|
|
|
proc_attr_base = proc_attr_base_unavailable;
|
2020-11-02 17:16:55 -08:00
|
|
|
} else {
|
|
|
|
proc_attr_base = proc_attr_base_old;
|
2020-04-26 14:06:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 11:09:22 -07:00
|
|
|
static char *procattr_path(pid_t pid, const char *attr)
|
|
|
|
{
|
|
|
|
char *path = NULL;
|
2020-04-26 14:06:38 -07:00
|
|
|
|
|
|
|
/* ignore failure, we just fallback to the default value */
|
|
|
|
(void) pthread_once(&proc_attr_base_ctl, proc_attr_base_init_once);
|
|
|
|
if (asprintf(&path, proc_attr_base, pid, attr) > 0)
|
2011-07-21 11:09:22 -07:00
|
|
|
return path;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:16:55 -08:00
|
|
|
static int procattr_open(pid_t tid, const char *attr, int flags)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
tmp = procattr_path(tid, attr);
|
|
|
|
if (!tmp) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fd = open(tmp, flags);
|
|
|
|
free(tmp);
|
|
|
|
/* Test is we can fallback to a different interface this is ugly.
|
|
|
|
* If only the old interface is available:
|
|
|
|
* proc_attr_base == proc_attr_base_old - no fallback
|
|
|
|
* else if is_enabled()
|
|
|
|
* apparmor is available on the old interface
|
|
|
|
* we do NOT use is_private_enabled() as
|
|
|
|
* 1. the new private interface would have been tried first above
|
|
|
|
* 2. that can be true even when another LSM is using the
|
|
|
|
* old interface where is_enabled() is only successful if
|
|
|
|
* the old interface is available to apparmor.
|
|
|
|
*/
|
|
|
|
if (fd == -1 && errno == EACCES && proc_attr_base != proc_attr_base_old && is_enabled()) {
|
|
|
|
if (asprintf(&tmp, proc_attr_base_old, tid, attr) < 0)
|
|
|
|
return -1;
|
|
|
|
fd = open(tmp, flags);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2015-05-19 21:20:37 -05:00
|
|
|
/**
|
|
|
|
* parse_unconfined - check for the unconfined label
|
|
|
|
* @con: the confinement context
|
|
|
|
* @size: size of the confinement context (not including the NUL terminator)
|
|
|
|
*
|
|
|
|
* Returns: True if the con is the unconfined label or false otherwise
|
|
|
|
*/
|
|
|
|
static bool parse_unconfined(char *con, int size)
|
|
|
|
{
|
|
|
|
return size == UNCONFINED_SIZE &&
|
|
|
|
strncmp(con, UNCONFINED, UNCONFINED_SIZE) == 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:53:39 -07:00
|
|
|
/**
|
2015-05-19 21:20:51 -05:00
|
|
|
* splitcon - split the confinement context into a label and mode
|
2015-02-09 18:46:46 -06:00
|
|
|
* @con: the confinement context
|
2015-05-19 21:20:21 -05:00
|
|
|
* @size: size of the confinement context (not including the NUL terminator)
|
2015-05-19 21:31:53 -05:00
|
|
|
* @strip_newline: true if a trailing newline character should be stripped
|
2015-05-19 21:20:51 -05:00
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @con
|
|
|
|
* on success
|
2013-06-25 15:53:39 -07:00
|
|
|
*
|
2015-05-19 21:20:51 -05:00
|
|
|
* Modifies the @con string to split it into separate label and mode strings.
|
2015-05-19 21:31:53 -05:00
|
|
|
* If @strip_newline is true and @con contains a single trailing newline, it
|
|
|
|
* will be stripped on success (it will not be stripped on error). The @mode
|
|
|
|
* argument is optional. If @mode is NULL, @con will still be split between the
|
|
|
|
* label and mode (if present) but @mode will not be set.
|
2013-06-25 15:53:39 -07:00
|
|
|
*
|
2015-05-19 21:20:51 -05:00
|
|
|
* Returns: a pointer to the label string or NULL on error
|
2013-06-25 15:53:39 -07:00
|
|
|
*/
|
2015-05-19 21:31:53 -05:00
|
|
|
static char *splitcon(char *con, int size, bool strip_newline, char **mode)
|
2013-06-25 15:53:39 -07:00
|
|
|
{
|
2015-05-19 21:20:51 -05:00
|
|
|
char *label = NULL;
|
|
|
|
char *mode_str = NULL;
|
2015-05-19 21:31:53 -05:00
|
|
|
char *newline = NULL;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (strip_newline && con[size - 1] == '\n') {
|
|
|
|
newline = &con[size - 1];
|
|
|
|
size--;
|
|
|
|
}
|
2015-05-19 21:20:51 -05:00
|
|
|
|
|
|
|
if (parse_unconfined(con, size)) {
|
|
|
|
label = con;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 3 && con[size - 1] == ')') {
|
2015-05-19 21:20:21 -05:00
|
|
|
int pos = size - 2;
|
2013-06-25 15:53:39 -07:00
|
|
|
|
|
|
|
while (pos > 0 && !(con[pos] == ' ' && con[pos + 1] == '('))
|
|
|
|
pos--;
|
|
|
|
if (pos > 0) {
|
|
|
|
con[pos] = 0; /* overwrite ' ' */
|
2015-05-19 21:20:21 -05:00
|
|
|
con[size - 1] = 0; /* overwrite trailing ) */
|
2015-05-19 21:20:51 -05:00
|
|
|
mode_str = &con[pos + 2]; /* skip '(' */
|
|
|
|
label = con;
|
2013-06-25 15:53:39 -07:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 21:20:51 -05:00
|
|
|
out:
|
2015-05-19 21:31:53 -05:00
|
|
|
if (label && strip_newline && newline)
|
|
|
|
*newline = 0; /* overwrite '\n', if requested, on success */
|
2015-05-19 21:20:51 -05:00
|
|
|
if (mode)
|
|
|
|
*mode = mode_str;
|
|
|
|
return label;
|
2013-06-25 15:53:39 -07:00
|
|
|
}
|
|
|
|
|
2015-05-19 21:28:47 -05:00
|
|
|
/**
|
|
|
|
* aa_splitcon - split the confinement context into a label and mode
|
|
|
|
* @con: the confinement context
|
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @con
|
|
|
|
* on success
|
|
|
|
*
|
2015-05-19 21:31:53 -05:00
|
|
|
* Modifies the @con string to split it into separate label and mode strings. A
|
|
|
|
* single trailing newline character will be stripped from @con, if found. The
|
|
|
|
* @mode argument is optional. If @mode is NULL, @con will still be split
|
2015-05-19 21:28:47 -05:00
|
|
|
* between the label and mode (if present) but @mode will not be set.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to the label string or NULL on error
|
|
|
|
*/
|
|
|
|
char *aa_splitcon(char *con, char **mode)
|
|
|
|
{
|
2015-05-19 21:31:53 -05:00
|
|
|
return splitcon(con, strlen(con), true, mode);
|
2015-05-19 21:28:47 -05:00
|
|
|
}
|
|
|
|
|
2011-08-09 06:45:51 -07:00
|
|
|
/**
|
|
|
|
* aa_getprocattr_raw - get the contents of @attr for @tid into @buf
|
|
|
|
* @tid: tid of task to query
|
|
|
|
* @attr: which /proc/<tid>/attr/<attr> to query
|
|
|
|
* @buf: buffer to store the result in
|
|
|
|
* @len: size of the buffer
|
2013-09-04 15:48:43 -07:00
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @buf
|
2011-08-09 06:45:51 -07:00
|
|
|
*
|
|
|
|
* Returns: size of data read or -1 on error, and sets errno
|
|
|
|
*/
|
|
|
|
int aa_getprocattr_raw(pid_t tid, const char *attr, char *buf, int len,
|
|
|
|
char **mode)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
int fd, ret;
|
|
|
|
char *tmp = NULL;
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if (!buf || len <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:16:55 -08:00
|
|
|
fd = procattr_open(tid, attr, O_RDONLY);
|
2011-08-09 06:45:51 -07:00
|
|
|
if (fd == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = buf;
|
|
|
|
do {
|
|
|
|
ret = read(fd, tmp, len);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
tmp += ret;
|
|
|
|
size += ret;
|
|
|
|
len -= ret;
|
|
|
|
if (len < 0) {
|
|
|
|
errno = ERANGE;
|
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
} while (ret > 0);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
int saved;
|
|
|
|
if (ret != -1) {
|
|
|
|
errno = EPROTO;
|
|
|
|
}
|
|
|
|
saved = errno;
|
|
|
|
(void)close(fd);
|
|
|
|
errno = saved;
|
|
|
|
goto out;
|
|
|
|
} else if (size > 0 && buf[size - 1] != 0) {
|
|
|
|
/* check for null termination */
|
2015-05-19 21:31:53 -05:00
|
|
|
if (buf[size - 1] != '\n') {
|
|
|
|
if (len == 0) {
|
|
|
|
errno = ERANGE;
|
|
|
|
goto out2;
|
|
|
|
} else {
|
|
|
|
buf[size] = 0;
|
|
|
|
size++;
|
|
|
|
}
|
2011-08-09 06:45:51 -07:00
|
|
|
}
|
|
|
|
|
2015-05-19 21:31:53 -05:00
|
|
|
if (splitcon(buf, size, true, mode) != buf) {
|
2015-05-19 21:20:51 -05:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out2;
|
|
|
|
}
|
2011-08-09 06:45:51 -07:00
|
|
|
}
|
|
|
|
rc = size;
|
|
|
|
|
|
|
|
out2:
|
|
|
|
(void)close(fd);
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INITIAL_GUESS_SIZE 128
|
|
|
|
|
|
|
|
/**
|
2015-02-09 18:46:46 -06:00
|
|
|
* aa_getprocattr - get the contents of @attr for @tid into @label and @mode
|
2011-08-09 06:45:51 -07:00
|
|
|
* @tid: tid of task to query
|
|
|
|
* @attr: which /proc/<tid>/attr/<attr> to query
|
2015-02-09 18:46:46 -06:00
|
|
|
* @label: allocated buffer the label is stored in
|
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @label
|
2011-08-09 06:45:51 -07:00
|
|
|
*
|
|
|
|
* Returns: size of data read or -1 on error, and sets errno
|
2013-09-04 15:48:43 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Guarantees that @label and @mode are null terminated. The length returned
|
|
|
|
* is for all data including both @label and @mode, and maybe > than
|
|
|
|
* strlen(@label) even if @mode is NULL
|
2013-09-04 15:48:43 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Caller is responsible for freeing the buffer returned in @label. @mode is
|
|
|
|
* always contained within @label's buffer and so NEVER do free(@mode)
|
2011-08-09 06:45:51 -07:00
|
|
|
*/
|
2015-02-09 18:46:46 -06:00
|
|
|
int aa_getprocattr(pid_t tid, const char *attr, char **label, char **mode)
|
2011-08-09 06:45:51 -07:00
|
|
|
{
|
|
|
|
int rc, size = INITIAL_GUESS_SIZE/2;
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
2015-02-09 18:46:46 -06:00
|
|
|
if (!label) {
|
2011-08-09 06:45:51 -07:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2015-03-25 17:09:27 -05:00
|
|
|
char *tmp;
|
|
|
|
|
2011-08-09 06:45:51 -07:00
|
|
|
size <<= 1;
|
2015-03-25 17:09:27 -05:00
|
|
|
tmp = realloc(buffer, size);
|
|
|
|
if (!tmp) {
|
|
|
|
free(buffer);
|
2011-08-09 06:45:51 -07:00
|
|
|
return -1;
|
2015-03-25 17:09:27 -05:00
|
|
|
}
|
|
|
|
buffer = tmp;
|
2011-08-09 06:45:51 -07:00
|
|
|
memset(buffer, 0, size);
|
|
|
|
|
|
|
|
rc = aa_getprocattr_raw(tid, attr, buffer, size, mode);
|
|
|
|
} while (rc == -1 && errno == ERANGE);
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
free(buffer);
|
2015-02-09 18:46:46 -06:00
|
|
|
*label = NULL;
|
2013-07-02 11:47:43 -07:00
|
|
|
if (mode)
|
|
|
|
*mode = NULL;
|
2011-08-09 06:45:51 -07:00
|
|
|
} else
|
2015-02-09 18:46:46 -06:00
|
|
|
*label = buffer;
|
2011-08-09 06:45:51 -07:00
|
|
|
|
2012-03-22 07:58:18 -07:00
|
|
|
return rc;
|
2011-08-09 06:45:51 -07:00
|
|
|
}
|
|
|
|
|
2011-07-21 11:10:35 -07:00
|
|
|
static int setprocattr(pid_t tid, const char *attr, const char *buf, int len)
|
2007-07-28 15:41:04 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2011-07-21 11:09:22 -07:00
|
|
|
int fd, ret;
|
2007-07-28 15:41:04 +00:00
|
|
|
|
2007-09-15 05:41:44 +00:00
|
|
|
if (!buf) {
|
2007-07-28 15:41:04 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:16:55 -08:00
|
|
|
fd = procattr_open(tid, attr, O_WRONLY);
|
2007-07-28 15:41:04 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, buf, len);
|
|
|
|
if (ret != len) {
|
|
|
|
int saved;
|
|
|
|
if (ret != -1) {
|
|
|
|
errno = EPROTO;
|
|
|
|
}
|
|
|
|
saved = errno;
|
|
|
|
(void)close(fd);
|
|
|
|
errno = saved;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
(void)close(fd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
2007-08-16 04:19:54 +00:00
|
|
|
|
2007-08-16 04:26:19 +00:00
|
|
|
int aa_change_hat(const char *subprofile, unsigned long token)
|
|
|
|
{
|
2007-09-15 05:41:44 +00:00
|
|
|
int rc = -1;
|
|
|
|
int len = 0;
|
|
|
|
char *buf = NULL;
|
2014-01-23 13:16:56 -08:00
|
|
|
const char *fmt = "changehat %016lx^%s";
|
2007-09-15 05:41:44 +00:00
|
|
|
|
|
|
|
/* both may not be null */
|
|
|
|
if (!(token || subprofile)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subprofile && strnlen(subprofile, PATH_MAX + 1) > PATH_MAX) {
|
|
|
|
errno = EPROTO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asprintf(&buf, fmt, token, subprofile ? subprofile : "");
|
|
|
|
if (len < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-07-21 11:10:35 -07:00
|
|
|
rc = setprocattr(aa_gettid(), "current", buf, len);
|
2007-09-15 05:41:44 +00:00
|
|
|
out:
|
|
|
|
if (buf) {
|
|
|
|
/* clear local copy of magic token before freeing */
|
|
|
|
memset(buf, '\0', len);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
return rc;
|
2007-08-16 04:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* original change_hat interface */
|
|
|
|
int __change_hat(char *subprofile, unsigned int token)
|
|
|
|
{
|
|
|
|
return aa_change_hat(subprofile, (unsigned long) token);
|
|
|
|
}
|
|
|
|
|
2007-09-15 05:41:44 +00:00
|
|
|
int aa_change_profile(const char *profile)
|
2007-08-16 04:35:56 +00:00
|
|
|
{
|
2007-09-15 05:41:44 +00:00
|
|
|
char *buf = NULL;
|
|
|
|
int len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asprintf(&buf, "changeprofile %s", profile);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-21 11:10:35 -07:00
|
|
|
rc = setprocattr(aa_gettid(), "current", buf, len);
|
2010-02-11 15:37:25 -08:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int aa_change_onexec(const char *profile)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
int len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asprintf(&buf, "exec %s", profile);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-21 11:10:35 -07:00
|
|
|
rc = setprocattr(aa_gettid(), "exec", buf, len);
|
2007-09-15 05:41:44 +00:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return rc;
|
2007-08-16 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 04:19:54 +00:00
|
|
|
/* create an alias for the old change_hat@IMMUNIX_1.0 symbol */
|
|
|
|
extern typeof((__change_hat)) __old_change_hat __attribute__((alias ("__change_hat")));
|
|
|
|
symbol_version(__old_change_hat, change_hat, IMMUNIX_1.0);
|
|
|
|
default_symbol_version(__change_hat, change_hat, APPARMOR_1.0);
|
2010-02-11 15:38:24 -08:00
|
|
|
|
|
|
|
|
|
|
|
int aa_change_hatv(const char *subprofiles[], unsigned long token)
|
|
|
|
{
|
|
|
|
int size, totallen = 0, hatcount = 0;
|
|
|
|
int rc = -1;
|
|
|
|
const char **hats;
|
|
|
|
char *pos, *buf = NULL;
|
|
|
|
const char *cmd = "changehat";
|
|
|
|
|
|
|
|
/* both may not be null */
|
|
|
|
if (!token && !(subprofiles && *subprofiles)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate hat lengths and while we are at it count how many and
|
|
|
|
* mem required */
|
|
|
|
if (subprofiles) {
|
|
|
|
for (hats = subprofiles; *hats; hats++) {
|
|
|
|
int len = strnlen(*hats, PATH_MAX + 1);
|
|
|
|
if (len > PATH_MAX) {
|
|
|
|
errno = EPROTO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
totallen += len + 1;
|
|
|
|
hatcount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate size of cmd + space + token + ^ + vector of hats */
|
|
|
|
size = strlen(cmd) + 18 + totallen + 1;
|
|
|
|
buf = malloc(size);
|
|
|
|
if (!buf) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup command string which is of the form
|
|
|
|
* changehat <token>^hat1\0hat2\0hat3\0..\0
|
|
|
|
*/
|
2010-07-26 10:58:07 -07:00
|
|
|
sprintf(buf, "%s %016lx^", cmd, token);
|
2010-02-11 15:38:24 -08:00
|
|
|
pos = buf + strlen(buf);
|
|
|
|
if (subprofiles) {
|
|
|
|
for (hats = subprofiles; *hats; hats++) {
|
|
|
|
strcpy(pos, *hats);
|
|
|
|
pos += strlen(*hats) + 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* step pos past trailing \0 */
|
|
|
|
pos++;
|
|
|
|
|
2011-07-21 11:10:35 -07:00
|
|
|
rc = setprocattr(aa_gettid(), "current", buf, pos - buf);
|
2010-02-11 15:38:24 -08:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (buf) {
|
|
|
|
/* clear local copy of magic token before freeing */
|
|
|
|
memset(buf, '\0', size);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* change_hat_vargs - change_hatv but passing the hats as fn arguments
|
|
|
|
* @token: the magic token
|
|
|
|
* @nhat: the number of hats being passed in the arguments
|
|
|
|
* ...: a argument list of const char * being passed
|
|
|
|
*
|
|
|
|
* change_hat_vargs can be called directly but it is meant to be called
|
|
|
|
* through its macro wrapper of the same name. Which automatically
|
|
|
|
* fills in the nhats arguments based on the number of parameters
|
|
|
|
* passed.
|
|
|
|
* to call change_hat_vargs direction do
|
|
|
|
* (change_hat_vargs)(token, nhats, hat1, hat2...)
|
|
|
|
*/
|
|
|
|
int (aa_change_hat_vargs)(unsigned long token, int nhats, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *argv[nhats+1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_start(ap, nhats);
|
|
|
|
for (i = 0; i < nhats ; i++) {
|
|
|
|
argv[i] = va_arg(ap, char *);
|
|
|
|
}
|
|
|
|
argv[nhats] = NULL;
|
|
|
|
va_end(ap);
|
|
|
|
return aa_change_hatv(argv, token);
|
|
|
|
}
|
2011-08-09 06:47:40 -07:00
|
|
|
|
2016-03-18 17:28:50 -05:00
|
|
|
int aa_stack_profile(const char *profile)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
int len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asprintf(&buf, "stack %s", profile);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = setprocattr(aa_gettid(), "current", buf, len);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int aa_stack_onexec(const char *profile)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
int len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asprintf(&buf, "stack %s", profile);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = setprocattr(aa_gettid(), "exec", buf, len);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-08-09 06:47:40 -07:00
|
|
|
/**
|
2015-02-09 18:46:46 -06:00
|
|
|
* aa_gettaskcon - get the confinement context for task @target in an allocated buffer
|
2011-08-09 06:47:40 -07:00
|
|
|
* @target: task to query
|
2015-02-09 18:46:46 -06:00
|
|
|
* @label: pointer to returned buffer with the label
|
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @label
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Returns: length of confinement context or -1 on error and sets errno
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Guarantees that @label and @mode are null terminated. The length returned
|
|
|
|
* is for all data including both @label and @mode, and maybe > than
|
|
|
|
* strlen(@label) even if @mode is NULL
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Caller is responsible for freeing the buffer returned in @label. @mode is
|
|
|
|
* always contained within @label's buffer and so NEVER do free(@mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
*/
|
2015-02-09 18:46:46 -06:00
|
|
|
int aa_gettaskcon(pid_t target, char **label, char **mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
{
|
2015-02-09 18:46:46 -06:00
|
|
|
return aa_getprocattr(target, "current", label, mode);
|
2011-08-09 06:47:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-09 18:46:46 -06:00
|
|
|
* aa_getcon - get the confinement context for current task in an allocated buffer
|
|
|
|
* @label: pointer to return buffer with the label if successful
|
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @label
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Returns: length of confinement context or -1 on error and sets errno
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Guarantees that @label and @mode are null terminated. The length returned
|
|
|
|
* is for all data including both @label and @mode, and may > than
|
|
|
|
* strlen(@label) even if @mode is NULL
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Caller is responsible for freeing the buffer returned in @label. @mode is
|
|
|
|
* always contained within @label's buffer and so NEVER do free(@mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
*/
|
2015-02-09 18:46:46 -06:00
|
|
|
int aa_getcon(char **label, char **mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
{
|
2015-02-09 18:46:46 -06:00
|
|
|
return aa_gettaskcon(aa_gettid(), label, mode);
|
2011-08-09 06:47:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SO_PEERSEC
|
|
|
|
#define SO_PEERSEC 31
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2015-02-09 18:46:46 -06:00
|
|
|
* aa_getpeercon_raw - get the confinement context of the socket's peer (other end)
|
|
|
|
* @fd: socket to get peer confinement context for
|
2013-06-25 15:55:08 -07:00
|
|
|
* @buf: buffer to store the result in
|
|
|
|
* @len: initially contains size of the buffer, returns size of data read
|
2013-09-04 15:48:43 -07:00
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @buf
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Returns: length of confinement context including null termination or -1 on
|
|
|
|
* error if errno == ERANGE then @len will hold the size needed
|
2011-08-09 06:47:40 -07:00
|
|
|
*/
|
2020-06-03 01:07:26 -07:00
|
|
|
int aa_getpeercon_raw(int fd, char *buf, socklen_t *len, char **mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
{
|
2020-06-01 00:58:50 -07:00
|
|
|
socklen_t optlen;
|
2011-08-09 06:47:40 -07:00
|
|
|
int rc;
|
|
|
|
|
2020-06-01 00:58:50 -07:00
|
|
|
if (*len <= 0 || buf == NULL) {
|
2011-08-09 06:47:40 -07:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-03 01:07:26 -07:00
|
|
|
optlen = *len;
|
2011-08-09 06:47:40 -07:00
|
|
|
|
2020-04-27 05:21:58 -07:00
|
|
|
if (!is_enabled()) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* TODO: add check for private_enabled when alternate interface
|
|
|
|
* is approved
|
|
|
|
*/
|
2013-06-25 15:55:08 -07:00
|
|
|
rc = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &optlen);
|
2011-08-09 06:47:40 -07:00
|
|
|
if (rc == -1 || optlen <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* check for null termination */
|
2013-06-25 15:55:08 -07:00
|
|
|
if (buf[optlen - 1] != 0) {
|
2020-06-03 01:07:26 -07:00
|
|
|
if (optlen < *len) {
|
2013-06-25 15:55:08 -07:00
|
|
|
buf[optlen] = 0;
|
2011-08-09 06:47:40 -07:00
|
|
|
optlen++;
|
|
|
|
} else {
|
2013-06-25 15:55:08 -07:00
|
|
|
/* buf needs to be bigger by 1 */
|
2011-08-09 06:47:40 -07:00
|
|
|
rc = -1;
|
|
|
|
errno = ERANGE;
|
|
|
|
optlen++;
|
2013-06-25 15:54:17 -07:00
|
|
|
goto out;
|
2011-08-09 06:47:40 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 15:52:46 -07:00
|
|
|
|
2015-05-19 21:31:53 -05:00
|
|
|
if (splitcon(buf, optlen - 1, false, mode) != buf) {
|
2015-05-19 21:20:51 -05:00
|
|
|
rc = -1;
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-25 15:54:17 -07:00
|
|
|
|
2013-06-25 15:52:46 -07:00
|
|
|
rc = optlen;
|
2011-08-09 06:47:40 -07:00
|
|
|
out:
|
2013-06-25 15:55:08 -07:00
|
|
|
*len = optlen;
|
2011-08-09 06:47:40 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-09 18:46:46 -06:00
|
|
|
* aa_getpeercon - get the confinement context of the socket's peer (other end)
|
|
|
|
* @fd: socket to get peer confinement context for
|
|
|
|
* @label: pointer to allocated buffer with the label
|
|
|
|
* @mode: if non-NULL and a mode is present, will point to mode string in @label
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Returns: length of confinement context including null termination or -1 on error
|
2011-08-09 06:47:40 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Guarantees that @label and @mode are null terminated. The length returned
|
|
|
|
* is for all data including both @label and @mode, and maybe > than
|
|
|
|
* strlen(@label) even if @mode is NULL
|
2013-09-04 15:48:43 -07:00
|
|
|
*
|
2015-02-09 18:46:46 -06:00
|
|
|
* Caller is responsible for freeing the buffer returned in @label. @mode is
|
|
|
|
* always contained within @label's buffer and so NEVER do free(@mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
*/
|
2015-02-09 18:46:46 -06:00
|
|
|
int aa_getpeercon(int fd, char **label, char **mode)
|
2011-08-09 06:47:40 -07:00
|
|
|
{
|
2020-06-03 01:07:26 -07:00
|
|
|
socklen_t last_size, size = INITIAL_GUESS_SIZE;
|
|
|
|
int rc;
|
2011-08-09 06:47:40 -07:00
|
|
|
char *buffer = NULL;
|
|
|
|
|
2015-02-09 18:46:46 -06:00
|
|
|
if (!label) {
|
2011-08-09 06:47:40 -07:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2015-03-25 17:09:27 -05:00
|
|
|
char *tmp;
|
|
|
|
|
2013-06-25 15:52:15 -07:00
|
|
|
last_size = size;
|
2015-03-25 17:09:27 -05:00
|
|
|
tmp = realloc(buffer, size);
|
|
|
|
if (!tmp) {
|
|
|
|
free(buffer);
|
2011-08-09 06:47:40 -07:00
|
|
|
return -1;
|
2015-03-25 17:09:27 -05:00
|
|
|
}
|
|
|
|
buffer = tmp;
|
2011-08-09 06:47:40 -07:00
|
|
|
memset(buffer, 0, size);
|
|
|
|
|
2013-06-25 15:54:17 -07:00
|
|
|
rc = aa_getpeercon_raw(fd, buffer, &size, mode);
|
2013-06-25 15:52:15 -07:00
|
|
|
/* size should contain actual size needed if errno == ERANGE */
|
|
|
|
} while (rc == -1 && errno == ERANGE && size > last_size);
|
2011-08-09 06:47:40 -07:00
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
free(buffer);
|
2015-02-09 18:46:46 -06:00
|
|
|
*label = NULL;
|
2013-06-25 15:54:17 -07:00
|
|
|
if (mode)
|
|
|
|
*mode = NULL;
|
2011-08-09 06:47:40 -07:00
|
|
|
size = -1;
|
|
|
|
} else
|
2015-02-09 18:46:46 -06:00
|
|
|
*label = buffer;
|
2011-08-09 06:47:40 -07:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2013-07-31 09:22:40 -07:00
|
|
|
|
|
|
|
static pthread_once_t aafs_access_control = PTHREAD_ONCE_INIT;
|
|
|
|
static char *aafs_access = NULL;
|
|
|
|
|
|
|
|
static void aafs_access_init_once(void)
|
|
|
|
{
|
|
|
|
char *aafs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = aa_find_mountpoint(&aafs);
|
|
|
|
if (ret < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret = asprintf(&aafs_access, "%s/.access", aafs);
|
|
|
|
if (ret < 0)
|
|
|
|
aafs_access = NULL;
|
|
|
|
|
|
|
|
free(aafs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "allow 0x00000000\ndeny 0x00000000\naudit 0x00000000\nquiet 0x00000000\n" */
|
|
|
|
#define QUERY_LABEL_REPLY_LEN 67
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_query_label - query the access(es) of a label
|
|
|
|
* @mask: permission bits to query
|
|
|
|
* @query: binary query string, must be offset by AA_QUERY_CMD_LABEL_SIZE
|
|
|
|
* @size: size of the query string must include AA_QUERY_CMD_LABEL_SIZE
|
|
|
|
* @allowed: upon successful return, will be 1 if query is allowed and 0 if not
|
|
|
|
* @audited: upon successful return, will be 1 if query should be audited and 0
|
|
|
|
* if not
|
|
|
|
*
|
2013-08-09 18:55:16 -07:00
|
|
|
* Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
|
|
|
|
* ENOENT, the subject label in the query string is unknown to the
|
|
|
|
* kernel.
|
2013-07-31 09:22:40 -07:00
|
|
|
*/
|
2014-03-01 15:46:42 -08:00
|
|
|
int query_label(uint32_t mask, char *query, size_t size, int *allowed,
|
|
|
|
int *audited)
|
2013-07-31 09:22:40 -07:00
|
|
|
{
|
|
|
|
char buf[QUERY_LABEL_REPLY_LEN];
|
|
|
|
uint32_t allow, deny, audit, quiet;
|
|
|
|
int fd, ret, saved;
|
|
|
|
|
|
|
|
if (!mask || size <= AA_QUERY_CMD_LABEL_SIZE) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_once(&aafs_access_control, aafs_access_init_once);
|
|
|
|
if (ret) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
} else if (!aafs_access) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(aafs_access, O_RDWR);
|
2013-08-09 18:55:16 -07:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
errno = EPROTONOSUPPORT;
|
2013-07-31 09:22:40 -07:00
|
|
|
return -1;
|
2013-08-09 18:55:16 -07:00
|
|
|
}
|
2013-07-31 09:22:40 -07:00
|
|
|
|
|
|
|
memcpy(query, AA_QUERY_CMD_LABEL, AA_QUERY_CMD_LABEL_SIZE);
|
|
|
|
errno = 0;
|
|
|
|
ret = write(fd, query, size);
|
2020-06-01 00:58:50 -07:00
|
|
|
if (ret < 0 || ((size_t) ret != size)) {
|
2013-07-31 09:22:40 -07:00
|
|
|
if (ret >= 0)
|
|
|
|
errno = EPROTO;
|
2013-08-09 18:55:16 -07:00
|
|
|
/* IMPORTANT: This is the only valid error path that can have
|
|
|
|
* errno set to ENOENT. It indicates that the subject label
|
|
|
|
* could not be found by the kernel.
|
|
|
|
*/
|
2013-08-26 16:54:26 -07:00
|
|
|
(void)close(fd);
|
2013-07-31 09:22:40 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(fd, buf, QUERY_LABEL_REPLY_LEN);
|
|
|
|
saved = errno;
|
|
|
|
(void)close(fd);
|
|
|
|
errno = saved;
|
|
|
|
if (ret != QUERY_LABEL_REPLY_LEN) {
|
2013-08-09 18:55:16 -07:00
|
|
|
errno = EPROTO;
|
2013-07-31 09:22:40 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sscanf(buf, "allow 0x%8" SCNx32 "\n"
|
|
|
|
"deny 0x%8" SCNx32 "\n"
|
|
|
|
"audit 0x%8" SCNx32 "\n"
|
|
|
|
"quiet 0x%8" SCNx32 "\n",
|
|
|
|
&allow, &deny, &audit, &quiet);
|
|
|
|
if (ret != 4) {
|
|
|
|
errno = EPROTONOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*allowed = mask & ~(allow & ~deny) ? 0 : 1;
|
|
|
|
if (!(*allowed))
|
|
|
|
audit = 0xFFFFFFFF;
|
|
|
|
*audited = mask & ~(audit & ~quiet) ? 0 : 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-01 15:46:42 -08:00
|
|
|
|
|
|
|
/* export multiple aa_query_label symbols to compensate for downstream
|
|
|
|
* releases with differing symbol versions. */
|
|
|
|
extern typeof((query_label)) __aa_query_label __attribute__((alias ("query_label")));
|
|
|
|
symbol_version(__aa_query_label, aa_query_label, APPARMOR_1.1);
|
2014-03-01 16:19:11 -08:00
|
|
|
default_symbol_version(query_label, aa_query_label, APPARMOR_2.9);
|
2015-06-06 01:26:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_query_file_path_len - query access permissions for a file @path
|
|
|
|
* @mask: permission bits to query
|
|
|
|
* @label: apparmor label
|
|
|
|
* @label_len: length of @label (does not include any terminating nul byte)
|
|
|
|
* @path: file path to query permissions for
|
|
|
|
* @path_len: length of @path (does not include any terminating nul byte)
|
|
|
|
* @allowed: upon successful return, will be 1 if query is allowed and 0 if not
|
|
|
|
* @audited: upon successful return, will be 1 if query should be audited and 0
|
|
|
|
* if not
|
|
|
|
*
|
|
|
|
* Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
|
|
|
|
* ENOENT, the subject label in the query string is unknown to the
|
|
|
|
* kernel.
|
|
|
|
*/
|
|
|
|
int aa_query_file_path_len(uint32_t mask, const char *label, size_t label_len,
|
|
|
|
const char *path, size_t path_len, int *allowed,
|
|
|
|
int *audited)
|
|
|
|
{
|
|
|
|
autofree char *query = NULL;
|
|
|
|
|
|
|
|
/* + 1 for null separator */
|
|
|
|
size_t size = AA_QUERY_CMD_LABEL_SIZE + label_len + 1 + path_len;
|
|
|
|
query = malloc(size + 1);
|
|
|
|
if (!query)
|
|
|
|
return -1;
|
|
|
|
memcpy(query + AA_QUERY_CMD_LABEL_SIZE, label, label_len);
|
|
|
|
/* null separator */
|
|
|
|
query[AA_QUERY_CMD_LABEL_SIZE + label_len] = 0;
|
|
|
|
query[AA_QUERY_CMD_LABEL_SIZE + label_len + 1] = AA_CLASS_FILE;
|
|
|
|
memcpy(query + AA_QUERY_CMD_LABEL_SIZE + label_len + 2, path, path_len);
|
|
|
|
return aa_query_label(mask, query, size , allowed, audited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_query_file_path - query access permissions for a file @path
|
|
|
|
* @mask: permission bits to query
|
|
|
|
* @label: apparmor label
|
|
|
|
* @path: file path to query permissions for
|
|
|
|
* @allowed: upon successful return, will be 1 if query is allowed and 0 if not
|
|
|
|
* @audited: upon successful return, will be 1 if query should be audited and 0
|
|
|
|
* if not
|
|
|
|
*
|
|
|
|
* Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
|
|
|
|
* ENOENT, the subject label in the query string is unknown to the
|
|
|
|
* kernel.
|
|
|
|
*/
|
|
|
|
int aa_query_file_path(uint32_t mask, const char *label, const char *path,
|
|
|
|
int *allowed, int *audited)
|
|
|
|
{
|
|
|
|
return aa_query_file_path_len(mask, label, strlen(label), path,
|
|
|
|
strlen(path), allowed, audited);
|
|
|
|
}
|
2015-06-06 01:27:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_query_link_path_len - query access permissions for a hard link @link
|
|
|
|
* @label: apparmor label
|
|
|
|
* @label_len: length of @label (does not include any terminating nul byte)
|
|
|
|
* @target: file path that hard link will point to
|
|
|
|
* @target_len: length of @target (does not include any terminating nul byte)
|
|
|
|
* @link: file path of hard link
|
|
|
|
* @link_len: length of @link (does not include any terminating nul byte)
|
|
|
|
* @allowed: upon successful return, will be 1 if query is allowed and 0 if not
|
|
|
|
* @audited: upon successful return, will be 1 if query should be audited and 0
|
|
|
|
* if not
|
|
|
|
*
|
|
|
|
* Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
|
|
|
|
* ENOENT, the subject label in the query string is unknown to the
|
|
|
|
* kernel.
|
|
|
|
*/
|
|
|
|
int aa_query_link_path_len(const char *label, size_t label_len,
|
|
|
|
const char *target, size_t target_len,
|
|
|
|
const char *link, size_t link_len,
|
|
|
|
int *allowed, int *audited)
|
|
|
|
{
|
|
|
|
autofree char *query = NULL;
|
|
|
|
|
|
|
|
/* + 1 for null separators */
|
|
|
|
size_t size = AA_QUERY_CMD_LABEL_SIZE + label_len + 1 + target_len +
|
|
|
|
1 + link_len;
|
|
|
|
size_t pos = AA_QUERY_CMD_LABEL_SIZE;
|
|
|
|
|
|
|
|
query = malloc(size);
|
|
|
|
if (!query)
|
|
|
|
return -1;
|
|
|
|
memcpy(query + pos, label, label_len);
|
|
|
|
/* null separator */
|
|
|
|
pos += label_len;
|
|
|
|
query[pos] = 0;
|
|
|
|
query[++pos] = AA_CLASS_FILE;
|
|
|
|
memcpy(query + pos + 1, link, link_len);
|
|
|
|
/* The kernel does the query in two parts we could similate this
|
|
|
|
* doing the following, however as long as policy is compiled
|
|
|
|
* correctly this isn't requied, and it requires and extra round
|
|
|
|
* trip to the kernel and adds a race on policy replacement between
|
|
|
|
* the two queries.
|
|
|
|
*
|
2016-02-01 09:40:22 -06:00
|
|
|
int rc = aa_query_label(AA_MAY_LINK, query, size, allowed, audited);
|
2015-06-06 01:27:49 -07:00
|
|
|
if (rc || !*allowed)
|
|
|
|
return rc;
|
|
|
|
*/
|
|
|
|
pos += 1 + link_len;
|
|
|
|
query[pos] = 0;
|
|
|
|
memcpy(query + pos + 1, target, target_len);
|
|
|
|
return aa_query_label(AA_MAY_LINK, query, size, allowed, audited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aa_query_link_path - query access permissions for a hard link @link
|
|
|
|
* @label: apparmor label
|
|
|
|
* @target: file path that hard link will point to
|
|
|
|
* @link: file path of hard link
|
|
|
|
* @allowed: upon successful return, will be 1 if query is allowed and 0 if not
|
|
|
|
* @audited: upon successful return, will be 1 if query should be audited and 0
|
|
|
|
* if not
|
|
|
|
*
|
|
|
|
* Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
|
|
|
|
* ENOENT, the subject label in the query string is unknown to the
|
|
|
|
* kernel.
|
|
|
|
*/
|
|
|
|
int aa_query_link_path(const char *label, const char *target, const char *link,
|
|
|
|
int *allowed, int *audited)
|
|
|
|
{
|
|
|
|
return aa_query_link_path_len(label, strlen(label), target,
|
|
|
|
strlen(target), link, strlen(link),
|
|
|
|
allowed, audited);
|
|
|
|
}
|