Updated AppArmor with a newer backported AppArmor release by UBports
This commit is contained in:
@@ -12,8 +12,14 @@
|
||||
* License.
|
||||
*/
|
||||
|
||||
#include <linux/tty.h>
|
||||
#include <linux/fdtable.h>
|
||||
#include <linux/file.h>
|
||||
|
||||
#include "include/af_unix.h"
|
||||
#include "include/apparmor.h"
|
||||
#include "include/audit.h"
|
||||
#include "include/context.h"
|
||||
#include "include/file.h"
|
||||
#include "include/match.h"
|
||||
#include "include/path.h"
|
||||
@@ -22,6 +28,17 @@
|
||||
struct file_perms nullperms;
|
||||
|
||||
|
||||
static u32 map_mask_to_chr_mask(u32 mask)
|
||||
{
|
||||
u32 m = mask & PERMS_CHRS_MASK;
|
||||
if (mask & AA_MAY_GETATTR)
|
||||
m |= MAY_READ;
|
||||
if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
|
||||
m |= MAY_WRITE;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* audit_file_mask - convert mask to permission string
|
||||
* @buffer: buffer to write string to (NOT NULL)
|
||||
@@ -31,29 +48,7 @@ static void audit_file_mask(struct audit_buffer *ab, u32 mask)
|
||||
{
|
||||
char str[10];
|
||||
|
||||
char *m = str;
|
||||
|
||||
if (mask & AA_EXEC_MMAP)
|
||||
*m++ = 'm';
|
||||
if (mask & (MAY_READ | AA_MAY_META_READ))
|
||||
*m++ = 'r';
|
||||
if (mask & (MAY_WRITE | AA_MAY_META_WRITE | AA_MAY_CHMOD |
|
||||
AA_MAY_CHOWN))
|
||||
*m++ = 'w';
|
||||
else if (mask & MAY_APPEND)
|
||||
*m++ = 'a';
|
||||
if (mask & AA_MAY_CREATE)
|
||||
*m++ = 'c';
|
||||
if (mask & AA_MAY_DELETE)
|
||||
*m++ = 'd';
|
||||
if (mask & AA_MAY_LINK)
|
||||
*m++ = 'l';
|
||||
if (mask & AA_MAY_LOCK)
|
||||
*m++ = 'k';
|
||||
if (mask & MAY_EXEC)
|
||||
*m++ = 'x';
|
||||
*m = '\0';
|
||||
|
||||
aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
|
||||
audit_log_string(ab, str);
|
||||
}
|
||||
|
||||
@@ -67,24 +62,24 @@ static void file_audit_cb(struct audit_buffer *ab, void *va)
|
||||
struct common_audit_data *sa = va;
|
||||
kuid_t fsuid = current_fsuid();
|
||||
|
||||
if (sa->aad->fs.request & AA_AUDIT_FILE_MASK) {
|
||||
if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
|
||||
audit_log_format(ab, " requested_mask=");
|
||||
audit_file_mask(ab, sa->aad->fs.request);
|
||||
audit_file_mask(ab, aad(sa)->request);
|
||||
}
|
||||
if (sa->aad->fs.denied & AA_AUDIT_FILE_MASK) {
|
||||
if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
|
||||
audit_log_format(ab, " denied_mask=");
|
||||
audit_file_mask(ab, sa->aad->fs.denied);
|
||||
audit_file_mask(ab, aad(sa)->denied);
|
||||
}
|
||||
if (sa->aad->fs.request & AA_AUDIT_FILE_MASK) {
|
||||
if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
|
||||
audit_log_format(ab, " fsuid=%d",
|
||||
from_kuid(&init_user_ns, fsuid));
|
||||
audit_log_format(ab, " ouid=%d",
|
||||
from_kuid(&init_user_ns, sa->aad->fs.ouid));
|
||||
from_kuid(&init_user_ns, aad(sa)->fs.ouid));
|
||||
}
|
||||
|
||||
if (sa->aad->fs.target) {
|
||||
if (aad(sa)->target) {
|
||||
audit_log_format(ab, " target=");
|
||||
audit_log_untrustedstring(ab, sa->aad->fs.target);
|
||||
audit_log_untrustedstring(ab, aad(sa)->target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +87,6 @@ static void file_audit_cb(struct audit_buffer *ab, void *va)
|
||||
* aa_audit_file - handle the auditing of file operations
|
||||
* @profile: the profile being enforced (NOT NULL)
|
||||
* @perms: the permissions computed for the request (NOT NULL)
|
||||
* @gfp: allocation flags
|
||||
* @op: operation being mediated
|
||||
* @request: permissions requested
|
||||
* @name: name of object being mediated (MAYBE NULL)
|
||||
@@ -104,53 +98,85 @@ static void file_audit_cb(struct audit_buffer *ab, void *va)
|
||||
* Returns: %0 or error on failure
|
||||
*/
|
||||
int aa_audit_file(struct aa_profile *profile, struct file_perms *perms,
|
||||
gfp_t gfp, int op, u32 request, const char *name,
|
||||
const char *target, kuid_t ouid, const char *info, int error)
|
||||
int op, u32 request, const char *name, const char *target,
|
||||
kuid_t ouid, const char *info, int error)
|
||||
{
|
||||
int type = AUDIT_APPARMOR_AUTO;
|
||||
struct common_audit_data sa;
|
||||
struct apparmor_audit_data aad = {0,};
|
||||
sa.type = LSM_AUDIT_DATA_NONE;
|
||||
sa.aad = &aad;
|
||||
aad.op = op,
|
||||
aad.fs.request = request;
|
||||
aad.name = name;
|
||||
aad.fs.target = target;
|
||||
aad.fs.ouid = ouid;
|
||||
aad.info = info;
|
||||
aad.error = error;
|
||||
|
||||
if (likely(!sa.aad->error)) {
|
||||
DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op);
|
||||
aad(&sa)->request = request;
|
||||
aad(&sa)->name = name;
|
||||
aad(&sa)->target = target;
|
||||
aad(&sa)->fs.ouid = ouid;
|
||||
aad(&sa)->info = info;
|
||||
aad(&sa)->error = error;
|
||||
sa.u.tsk = NULL;
|
||||
|
||||
if (likely(!aad(&sa)->error)) {
|
||||
u32 mask = perms->audit;
|
||||
|
||||
if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
|
||||
mask = 0xffff;
|
||||
|
||||
/* mask off perms that are not being force audited */
|
||||
sa.aad->fs.request &= mask;
|
||||
aad(&sa)->request &= mask;
|
||||
|
||||
if (likely(!sa.aad->fs.request))
|
||||
if (likely(!aad(&sa)->request))
|
||||
return 0;
|
||||
type = AUDIT_APPARMOR_AUDIT;
|
||||
} else {
|
||||
/* only report permissions that were denied */
|
||||
sa.aad->fs.request = sa.aad->fs.request & ~perms->allow;
|
||||
aad(&sa)->request = aad(&sa)->request & ~perms->allow;
|
||||
|
||||
if (sa.aad->fs.request & perms->kill)
|
||||
if (aad(&sa)->request & perms->kill)
|
||||
type = AUDIT_APPARMOR_KILL;
|
||||
|
||||
/* quiet known rejects, assumes quiet and kill do not overlap */
|
||||
if ((sa.aad->fs.request & perms->quiet) &&
|
||||
if ((aad(&sa)->request & perms->quiet) &&
|
||||
AUDIT_MODE(profile) != AUDIT_NOQUIET &&
|
||||
AUDIT_MODE(profile) != AUDIT_ALL)
|
||||
sa.aad->fs.request &= ~perms->quiet;
|
||||
aad(&sa)->request &= ~perms->quiet;
|
||||
|
||||
if (!sa.aad->fs.request)
|
||||
return COMPLAIN_MODE(profile) ? 0 : sa.aad->error;
|
||||
if (!aad(&sa)->request)
|
||||
return COMPLAIN_MODE(profile) ? 0 : aad(&sa)->error;
|
||||
}
|
||||
|
||||
sa.aad->fs.denied = sa.aad->fs.request & ~perms->allow;
|
||||
return aa_audit(type, profile, gfp, &sa, file_audit_cb);
|
||||
aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
|
||||
return aa_audit(type, profile, &sa, file_audit_cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* is_deleted - test if a file has been completely unlinked
|
||||
* @dentry: dentry of file to test for deletion (NOT NULL)
|
||||
*
|
||||
* Returns: %1 if deleted else %0
|
||||
*/
|
||||
static inline bool is_deleted(struct dentry *dentry)
|
||||
{
|
||||
if (d_unlinked(dentry) && dentry->d_inode->i_nlink == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int path_name(int op, struct aa_label *label, struct path *path,
|
||||
int flags, char *buffer, const char**name,
|
||||
struct path_cond *cond, u32 request, bool delegate_deleted)
|
||||
{
|
||||
struct aa_profile *profile;
|
||||
const char *info = NULL;
|
||||
int error = aa_path_name(path, flags, buffer, name, &info,
|
||||
labels_profile(label)->disconnected);
|
||||
if (error) {
|
||||
if (error == -ENOENT && is_deleted(path->dentry) &&
|
||||
delegate_deleted)
|
||||
return 0;
|
||||
fn_for_each_confined(label, profile,
|
||||
aa_audit_file(profile, &nullperms, op, request, *name,
|
||||
NULL, cond->uid, info, error));
|
||||
return error;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,10 +189,11 @@ static u32 map_old_perms(u32 old)
|
||||
{
|
||||
u32 new = old & 0xf;
|
||||
if (old & MAY_READ)
|
||||
new |= AA_MAY_META_READ;
|
||||
new |= AA_MAY_GETATTR | AA_MAY_OPEN;
|
||||
if (old & MAY_WRITE)
|
||||
new |= AA_MAY_META_WRITE | AA_MAY_CREATE | AA_MAY_DELETE |
|
||||
AA_MAY_CHMOD | AA_MAY_CHOWN;
|
||||
new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
|
||||
AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN |
|
||||
AA_MAY_DELETE;
|
||||
if (old & 0x10)
|
||||
new |= AA_MAY_LINK;
|
||||
/* the old mapping lock and link_subset flags where overlaid
|
||||
@@ -214,7 +241,7 @@ static struct file_perms compute_perms(struct aa_dfa *dfa, unsigned int state,
|
||||
perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
|
||||
perms.xindex = dfa_other_xindex(dfa, state);
|
||||
}
|
||||
perms.allow |= AA_MAY_META_READ;
|
||||
perms.allow |= AA_MAY_GETATTR;
|
||||
|
||||
/* change_profile wasn't determined by ownership in old mapping */
|
||||
if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
|
||||
@@ -251,23 +278,25 @@ unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* is_deleted - test if a file has been completely unlinked
|
||||
* @dentry: dentry of file to test for deletion (NOT NULL)
|
||||
*
|
||||
* Returns: %1 if deleted else %0
|
||||
*/
|
||||
static inline bool is_deleted(struct dentry *dentry)
|
||||
int __aa_path_perm(int op, struct aa_profile *profile, const char *name,
|
||||
u32 request, struct path_cond *cond, int flags,
|
||||
struct file_perms *perms)
|
||||
{
|
||||
if (d_unlinked(dentry) && dentry->d_inode->i_nlink == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
int e = 0;
|
||||
if (profile_unconfined(profile) ||
|
||||
((flags & PATH_SOCK_COND) && !PROFILE_MEDIATES_AF(profile, AF_UNIX)))
|
||||
return 0;
|
||||
aa_str_perms(profile->file.dfa, profile->file.start, name, cond, perms);
|
||||
if (request & ~perms->allow)
|
||||
e = -EACCES;
|
||||
return aa_audit_file(profile, perms, op, request, name, NULL,
|
||||
cond->uid, NULL, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* aa_path_perm - do permissions check & audit for @path
|
||||
* @op: operation being checked
|
||||
* @profile: profile being enforced (NOT NULL)
|
||||
* @label: profile being enforced (NOT NULL)
|
||||
* @path: path to check permissions of (NOT NULL)
|
||||
* @flags: any additional path flags beyond what the profile specifies
|
||||
* @request: requested permissions
|
||||
@@ -275,35 +304,28 @@ static inline bool is_deleted(struct dentry *dentry)
|
||||
*
|
||||
* Returns: %0 else error if access denied or other error
|
||||
*/
|
||||
int aa_path_perm(int op, struct aa_profile *profile, struct path *path,
|
||||
int aa_path_perm(int op, struct aa_label *label, struct path *path,
|
||||
int flags, u32 request, struct path_cond *cond)
|
||||
{
|
||||
char *buffer = NULL;
|
||||
struct file_perms perms = {};
|
||||
const char *name, *info = NULL;
|
||||
char *buffer = NULL;
|
||||
const char *name;
|
||||
struct aa_profile *profile;
|
||||
int error;
|
||||
|
||||
flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
|
||||
error = aa_path_name(path, flags, &buffer, &name, &info);
|
||||
if (error) {
|
||||
if (error == -ENOENT && is_deleted(path->dentry)) {
|
||||
/* Access to open files that are deleted are
|
||||
* give a pass (implicit delegation)
|
||||
*/
|
||||
error = 0;
|
||||
info = NULL;
|
||||
perms.allow = request;
|
||||
}
|
||||
} else {
|
||||
aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
|
||||
&perms);
|
||||
if (request & ~perms.allow)
|
||||
error = -EACCES;
|
||||
}
|
||||
error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request, name,
|
||||
NULL, cond->uid, info, error);
|
||||
kfree(buffer);
|
||||
/* TODO: fix path lookup flags */
|
||||
flags |= labels_profile(label)->path_flags |
|
||||
(S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
|
||||
get_buffers(buffer);
|
||||
|
||||
error = path_name(op, label, path, flags, buffer, &name, cond,
|
||||
request, true);
|
||||
if (!error)
|
||||
error = fn_for_each_confined(label, profile,
|
||||
__aa_path_perm(op, profile, name, request, cond,
|
||||
flags, &perms));
|
||||
|
||||
put_buffers(buffer);
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -327,65 +349,25 @@ static inline bool xindex_is_subset(u32 link, u32 target)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* aa_path_link - Handle hard link permission check
|
||||
* @profile: the profile being enforced (NOT NULL)
|
||||
* @old_dentry: the target dentry (NOT NULL)
|
||||
* @new_dir: directory the new link will be created in (NOT NULL)
|
||||
* @new_dentry: the link being created (NOT NULL)
|
||||
*
|
||||
* Handle the permission test for a link & target pair. Permission
|
||||
* is encoded as a pair where the link permission is determined
|
||||
* first, and if allowed, the target is tested. The target test
|
||||
* is done from the point of the link match (not start of DFA)
|
||||
* making the target permission dependent on the link permission match.
|
||||
*
|
||||
* The subset test if required forces that permissions granted
|
||||
* on link are a subset of the permission granted to target.
|
||||
*
|
||||
* Returns: %0 if allowed else error
|
||||
*/
|
||||
int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
|
||||
struct path *new_dir, struct dentry *new_dentry)
|
||||
static int profile_path_link(struct aa_profile *profile, const char *lname,
|
||||
const char *tname, struct path_cond *cond)
|
||||
{
|
||||
struct path link = { new_dir->mnt, new_dentry };
|
||||
struct path target = { new_dir->mnt, old_dentry };
|
||||
struct path_cond cond = {
|
||||
old_dentry->d_inode->i_uid,
|
||||
old_dentry->d_inode->i_mode
|
||||
};
|
||||
char *buffer = NULL, *buffer2 = NULL;
|
||||
const char *lname, *tname = NULL, *info = NULL;
|
||||
struct file_perms lperms, perms;
|
||||
const char *info = NULL;
|
||||
u32 request = AA_MAY_LINK;
|
||||
unsigned int state;
|
||||
int error;
|
||||
int e = -EACCES;
|
||||
|
||||
lperms = nullperms;
|
||||
|
||||
/* buffer freed below, lname is pointer in buffer */
|
||||
error = aa_path_name(&link, profile->path_flags, &buffer, &lname,
|
||||
&info);
|
||||
if (error)
|
||||
goto audit;
|
||||
|
||||
/* buffer2 freed below, tname is pointer in buffer2 */
|
||||
error = aa_path_name(&target, profile->path_flags, &buffer2, &tname,
|
||||
&info);
|
||||
if (error)
|
||||
goto audit;
|
||||
|
||||
error = -EACCES;
|
||||
/* aa_str_perms - handles the case of the dfa being NULL */
|
||||
state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
|
||||
&cond, &lperms);
|
||||
cond, &lperms);
|
||||
|
||||
if (!(lperms.allow & AA_MAY_LINK))
|
||||
goto audit;
|
||||
|
||||
/* test to see if target can be paired with link */
|
||||
state = aa_dfa_null_transition(profile->file.dfa, state);
|
||||
aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
|
||||
aa_str_perms(profile->file.dfa, state, tname, cond, &perms);
|
||||
|
||||
/* force audit/quiet masks for link are stored in the second entry
|
||||
* in the link pair.
|
||||
@@ -403,10 +385,10 @@ int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
|
||||
if (!(perms.allow & AA_LINK_SUBSET))
|
||||
goto done_tests;
|
||||
|
||||
/* Do link perm subset test requiring allowed permission on link are a
|
||||
* subset of the allowed permissions on target.
|
||||
/* Do link perm subset test requiring allowed permission on link are
|
||||
* a subset of the allowed permissions on target.
|
||||
*/
|
||||
aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
|
||||
aa_str_perms(profile->file.dfa, profile->file.start, tname, cond,
|
||||
&perms);
|
||||
|
||||
/* AA_MAY_LINK is not considered in the subset test */
|
||||
@@ -425,13 +407,175 @@ int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
|
||||
}
|
||||
|
||||
done_tests:
|
||||
error = 0;
|
||||
e = 0;
|
||||
|
||||
audit:
|
||||
error = aa_audit_file(profile, &lperms, GFP_KERNEL, OP_LINK, request,
|
||||
lname, tname, cond.uid, info, error);
|
||||
kfree(buffer);
|
||||
kfree(buffer2);
|
||||
return aa_audit_file(profile, &lperms, OP_LINK, request, lname, tname,
|
||||
cond->uid, info, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* aa_path_link - Handle hard link permission check
|
||||
* @label: the label being enforced (NOT NULL)
|
||||
* @old_dentry: the target dentry (NOT NULL)
|
||||
* @new_dir: directory the new link will be created in (NOT NULL)
|
||||
* @new_dentry: the link being created (NOT NULL)
|
||||
*
|
||||
* Handle the permission test for a link & target pair. Permission
|
||||
* is encoded as a pair where the link permission is determined
|
||||
* first, and if allowed, the target is tested. The target test
|
||||
* is done from the point of the link match (not start of DFA)
|
||||
* making the target permission dependent on the link permission match.
|
||||
*
|
||||
* The subset test if required forces that permissions granted
|
||||
* on link are a subset of the permission granted to target.
|
||||
*
|
||||
* Returns: %0 if allowed else error
|
||||
*/
|
||||
int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
|
||||
struct path *new_dir, struct dentry *new_dentry)
|
||||
{
|
||||
struct path link = { new_dir->mnt, new_dentry };
|
||||
struct path target = { new_dir->mnt, old_dentry };
|
||||
struct path_cond cond = {
|
||||
old_dentry->d_inode->i_uid,
|
||||
old_dentry->d_inode->i_mode
|
||||
};
|
||||
char *buffer = NULL, *buffer2 = NULL;
|
||||
const char *lname, *tname = NULL;
|
||||
struct aa_profile *profile;
|
||||
int error;
|
||||
|
||||
/* TODO: fix path lookup flags, auditing of failed path for profile */
|
||||
profile = labels_profile(label);
|
||||
/* buffer freed below, lname is pointer in buffer */
|
||||
get_buffers(buffer, buffer2);
|
||||
error = path_name(OP_LINK, label, &link,
|
||||
labels_profile(label)->path_flags, buffer,
|
||||
&lname, &cond, AA_MAY_LINK, false);
|
||||
if (error)
|
||||
goto out;
|
||||
|
||||
/* buffer2 freed below, tname is pointer in buffer2 */
|
||||
error = path_name(OP_LINK, label, &target,
|
||||
labels_profile(label)->path_flags, buffer2, &tname,
|
||||
&cond, AA_MAY_LINK, false);
|
||||
if (error)
|
||||
goto out;
|
||||
|
||||
error = fn_for_each_confined(label, profile,
|
||||
profile_path_link(profile, lname, tname, &cond));
|
||||
|
||||
out:
|
||||
put_buffers(buffer, buffer2);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static void update_file_cxt(struct aa_file_cxt *fcxt, struct aa_label *label,
|
||||
u32 request)
|
||||
{
|
||||
struct aa_label *l, *old;
|
||||
|
||||
/* update caching of label on file_cxt */
|
||||
spin_lock(&fcxt->lock);
|
||||
old = rcu_dereference_protected(fcxt->label,
|
||||
spin_is_locked(&fcxt->lock));
|
||||
l = aa_label_merge(old, label, GFP_ATOMIC);
|
||||
if (l) {
|
||||
if (l != old) {
|
||||
rcu_assign_pointer(fcxt->label, l);
|
||||
aa_put_label(old);
|
||||
} else
|
||||
aa_put_label(l);
|
||||
fcxt->allow |= request;
|
||||
}
|
||||
spin_unlock(&fcxt->lock);
|
||||
}
|
||||
|
||||
static int __file_path_perm(int op, struct aa_label *label,
|
||||
struct aa_label *flabel, struct file *file,
|
||||
u32 request, u32 denied)
|
||||
{
|
||||
struct aa_profile *profile;
|
||||
struct file_perms perms = {};
|
||||
struct path_cond cond = {
|
||||
.uid = file_inode(file)->i_uid,
|
||||
.mode = file_inode(file)->i_mode
|
||||
};
|
||||
const char *name;
|
||||
char *buffer;
|
||||
int flags, error;
|
||||
|
||||
/* revalidation due to label out of date. No revocation at this time */
|
||||
if (!denied && aa_label_is_subset(flabel, label))
|
||||
/* TODO: check for revocation on stale profiles */
|
||||
return 0;
|
||||
|
||||
/* TODO: fix path lookup flags */
|
||||
flags = PATH_DELEGATE_DELETED | labels_profile(label)->path_flags |
|
||||
(S_ISDIR(cond.mode) ? PATH_IS_DIR : 0);
|
||||
get_buffers(buffer);
|
||||
|
||||
error = path_name(op, label, &file->f_path, flags, buffer, &name, &cond,
|
||||
request, true);
|
||||
if (error) {
|
||||
if (error == 1)
|
||||
/* Access to open files that are deleted are
|
||||
* given a pass (implicit delegation)
|
||||
*/
|
||||
/* TODO not needed when full perms cached */
|
||||
error = 0;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* check every profile in task label not in current cache */
|
||||
error = fn_for_each_not_in_set(flabel, label, profile,
|
||||
__aa_path_perm(op, profile, name, request, &cond, 0,
|
||||
&perms));
|
||||
if (denied) {
|
||||
/* check every profile in file label that was not tested
|
||||
* in the initial check above.
|
||||
*/
|
||||
/* TODO: cache full perms so this only happens because of
|
||||
* conditionals */
|
||||
/* TODO: don't audit here */
|
||||
last_error(error,
|
||||
fn_for_each_not_in_set(label, flabel, profile,
|
||||
__aa_path_perm(op, profile, name, request,
|
||||
&cond, 0, &perms)));
|
||||
}
|
||||
if (!error)
|
||||
update_file_cxt(file_cxt(file), label, request);
|
||||
|
||||
out:
|
||||
put_buffers(buffer);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static int __file_sock_perm(int op, struct aa_label *label,
|
||||
struct aa_label *flabel, struct file *file,
|
||||
u32 request, u32 denied)
|
||||
{
|
||||
struct socket *sock = (struct socket *) file->private_data;
|
||||
int error;
|
||||
|
||||
AA_BUG(!sock);
|
||||
|
||||
/* revalidation due to label out of date. No revocation at this time */
|
||||
if (!denied && aa_label_is_subset(flabel, label))
|
||||
return 0;
|
||||
|
||||
/* TODO: improve to skip profiles cached in flabel */
|
||||
error = aa_sock_file_perm(label, op, request, sock);
|
||||
if (denied) {
|
||||
/* TODO: improve to skip profiles checked above */
|
||||
/* check every profile in file label to is cached */
|
||||
last_error(error, aa_sock_file_perm(flabel, op, request, sock));
|
||||
}
|
||||
if (!error)
|
||||
update_file_cxt(file_cxt(file), label, request);
|
||||
|
||||
return error;
|
||||
}
|
||||
@@ -439,20 +583,117 @@ audit:
|
||||
/**
|
||||
* aa_file_perm - do permission revalidation check & audit for @file
|
||||
* @op: operation being checked
|
||||
* @profile: profile being enforced (NOT NULL)
|
||||
* @label: label being enforced (NOT NULL)
|
||||
* @file: file to revalidate access permissions on (NOT NULL)
|
||||
* @request: requested permissions
|
||||
*
|
||||
* Returns: %0 if access allowed else error
|
||||
*/
|
||||
int aa_file_perm(int op, struct aa_profile *profile, struct file *file,
|
||||
int aa_file_perm(int op, struct aa_label *label, struct file *file,
|
||||
u32 request)
|
||||
{
|
||||
struct path_cond cond = {
|
||||
.uid = file_inode(file)->i_uid,
|
||||
.mode = file_inode(file)->i_mode
|
||||
};
|
||||
struct aa_file_cxt *fcxt;
|
||||
struct aa_label *flabel;
|
||||
u32 denied;
|
||||
int error = 0;
|
||||
|
||||
return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
|
||||
request, &cond);
|
||||
AA_BUG(!label);
|
||||
AA_BUG(!file);
|
||||
|
||||
fcxt = file_cxt(file);
|
||||
|
||||
rcu_read_lock();
|
||||
flabel = rcu_dereference(fcxt->label);
|
||||
AA_BUG(!flabel);
|
||||
|
||||
/* revalidate access, if task is unconfined, or the cached cred
|
||||
* doesn't match or if the request is for more permissions than
|
||||
* was granted.
|
||||
*
|
||||
* Note: the test for !unconfined(flabel) is to handle file
|
||||
* delegation from unconfined tasks
|
||||
*/
|
||||
denied = request & ~fcxt->allow;
|
||||
if (unconfined(label) || unconfined(flabel) ||
|
||||
(!denied && aa_label_is_subset(flabel, label)))
|
||||
goto done;
|
||||
|
||||
/* TODO: label cross check */
|
||||
|
||||
if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry)) {
|
||||
error = __file_path_perm(op, label, flabel, file, request,
|
||||
denied);
|
||||
|
||||
} else if (S_ISSOCK(file_inode(file)->i_mode)) {
|
||||
error = __file_sock_perm(op, label, flabel, file, request,
|
||||
denied);
|
||||
}
|
||||
done:
|
||||
rcu_read_unlock();
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static void revalidate_tty(struct aa_label *label)
|
||||
{
|
||||
struct tty_struct *tty;
|
||||
int drop_tty = 0;
|
||||
|
||||
tty = get_current_tty();
|
||||
if (!tty)
|
||||
return;
|
||||
|
||||
spin_lock(&tty_files_lock);
|
||||
if (!list_empty(&tty->tty_files)) {
|
||||
struct tty_file_private *file_priv;
|
||||
struct file *file;
|
||||
/* TODO: Revalidate access to controlling tty. */
|
||||
file_priv = list_first_entry(&tty->tty_files,
|
||||
struct tty_file_private, list);
|
||||
file = file_priv->file;
|
||||
|
||||
if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE))
|
||||
drop_tty = 1;
|
||||
}
|
||||
spin_unlock(&tty_files_lock);
|
||||
tty_kref_put(tty);
|
||||
|
||||
if (drop_tty)
|
||||
no_tty();
|
||||
}
|
||||
|
||||
static int match_file(const void *p, struct file *file, unsigned fd)
|
||||
{
|
||||
struct aa_label *label = (struct aa_label *)p;
|
||||
if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file)))
|
||||
return fd + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* based on selinux's flush_unauthorized_files */
|
||||
void aa_inherit_files(const struct cred *cred, struct files_struct *files)
|
||||
{
|
||||
struct aa_label *label = aa_get_newest_cred_label(cred);
|
||||
struct file *devnull = NULL;
|
||||
unsigned n;
|
||||
|
||||
revalidate_tty(label);
|
||||
|
||||
/* Revalidate access to inherited open files. */
|
||||
n = iterate_fd(files, 0, match_file, label);
|
||||
if (!n) /* none found? */
|
||||
goto out;
|
||||
|
||||
devnull = dentry_open(&aa_null, O_RDWR, cred);
|
||||
if (IS_ERR(devnull))
|
||||
devnull = NULL;
|
||||
/* replace all the matching ones with this */
|
||||
do {
|
||||
replace_fd(n - 1, devnull, 0);
|
||||
} while ((n = iterate_fd(files, n, match_file, label)) != 0);
|
||||
if (devnull)
|
||||
fput(devnull);
|
||||
out:
|
||||
aa_put_label(label);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user