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>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2010-02-11 15:38:24 -08:00
|
|
|
#include <stdarg.h>
|
2007-07-28 15:41:04 +00: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)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-02-11 15:37:25 -08:00
|
|
|
static int setprocattr(const char *path, const char *buf, int len)
|
2007-07-28 15:41:04 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2007-09-15 05:41:44 +00:00
|
|
|
int fd, ret, ctlerr = 0;
|
2007-07-28 15:41:04 +00:00
|
|
|
char *ctl = NULL;
|
2011-07-21 11:06:57 -07:00
|
|
|
pid_t tid = aa_gettid();
|
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;
|
|
|
|
}
|
|
|
|
|
2010-02-11 15:37:25 -08:00
|
|
|
ctlerr = asprintf(&ctl, path, tid);
|
2007-07-28 15:41:04 +00:00
|
|
|
if (ctlerr < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(ctl, O_WRONLY);
|
|
|
|
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:
|
|
|
|
if (ctl) {
|
|
|
|
free(ctl);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
const char *fmt = "changehat %016x^%s";
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2010-02-11 15:37:25 -08:00
|
|
|
rc = setprocattr("/proc/%d/attr/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;
|
|
|
|
|
2010-02-11 15:37:25 -08:00
|
|
|
rc = setprocattr("/proc/%d/attr/current", buf, len);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
rc = setprocattr("/proc/%d/attr/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++;
|
|
|
|
|
|
|
|
rc = setprocattr("/proc/%d/attr/current", buf, pos - buf);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|