Skip to content

Commit

Permalink
audit: use inline function to get audit context
Browse files Browse the repository at this point in the history
Recognizing that the audit context is an internal audit value, use an
access function to retrieve the audit context pointer for the task
rather than reaching directly into the task struct to get it.

Signed-off-by: Richard Guy Briggs <[email protected]>
[PM: merge fuzz in auditsc.c and selinuxfs.c, checkpatch.pl fixes]
Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
rgbriggs authored and pcmoore committed May 14, 2018
1 parent f0b7521 commit cdfb6b3
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 69 deletions.
15 changes: 12 additions & 3 deletions include/linux/audit.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,14 @@ extern void audit_seccomp_actions_logged(const char *names,
const char *old_names, int res);
extern void __audit_ptrace(struct task_struct *t);

static inline struct audit_context *audit_context(void)
{
return current->audit_context;
}

static inline bool audit_dummy_context(void)
{
void *p = current->audit_context;
void *p = audit_context();
return !p || *(int *)p;
}
static inline void audit_free(struct task_struct *task)
Expand All @@ -251,12 +256,12 @@ static inline void audit_syscall_entry(int major, unsigned long a0,
unsigned long a1, unsigned long a2,
unsigned long a3)
{
if (unlikely(current->audit_context))
if (unlikely(audit_context()))
__audit_syscall_entry(major, a0, a1, a2, a3);
}
static inline void audit_syscall_exit(void *pt_regs)
{
if (unlikely(current->audit_context)) {
if (unlikely(audit_context())) {
int success = is_syscall_success(pt_regs);
long return_code = regs_return_value(pt_regs);

Expand Down Expand Up @@ -464,6 +469,10 @@ static inline bool audit_dummy_context(void)
{
return true;
}
static inline struct audit_context *audit_context(void)
{
return NULL;
}
static inline struct filename *audit_reusename(const __user char *name)
{
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion include/net/xfrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ static inline struct audit_buffer *xfrm_audit_start(const char *op)

if (audit_enabled == 0)
return NULL;
audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
audit_buf = audit_log_start(audit_context(), GFP_ATOMIC,
AUDIT_MAC_IPSEC_EVENT);
if (audit_buf == NULL)
return NULL;
Expand Down
6 changes: 2 additions & 4 deletions kernel/audit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,8 +1099,7 @@ static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature

if (audit_enabled == AUDIT_OFF)
return;
ab = audit_log_start(current->audit_context,
GFP_KERNEL, AUDIT_FEATURE_CHANGE);
ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_FEATURE_CHANGE);
if (!ab)
return;
audit_log_task_info(ab, current);
Expand Down Expand Up @@ -2317,8 +2316,7 @@ void audit_log_link_denied(const char *operation)
return;

/* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
ab = audit_log_start(current->audit_context, GFP_KERNEL,
AUDIT_ANOM_LINK);
ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_LINK);
if (!ab)
return;
audit_log_format(ab, "op=%s", operation);
Expand Down
2 changes: 1 addition & 1 deletion kernel/audit_watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static void audit_update_watch(struct audit_parent *parent,
/* If the update involves invalidating rules, do the inode-based
* filtering now, so we don't omit records. */
if (invalidating && !audit_dummy_context())
audit_filter_inodes(current, current->audit_context);
audit_filter_inodes(current, audit_context());

/* updating ino will likely change which audit_hash_list we
* are on so we need a new watch for the new list */
Expand Down
66 changes: 32 additions & 34 deletions kernel/auditsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1509,8 +1509,7 @@ void __audit_free(struct task_struct *tsk)
void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
unsigned long a3, unsigned long a4)
{
struct task_struct *tsk = current;
struct audit_context *context = tsk->audit_context;
struct audit_context *context = audit_context();
enum audit_state state;

if (!audit_enabled || !context)
Expand All @@ -1525,7 +1524,7 @@ void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
context->dummy = !audit_n_rules;
if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
context->prio = 0;
if (auditd_test_task(tsk))
if (auditd_test_task(current))
return;
}

Expand Down Expand Up @@ -1563,12 +1562,12 @@ void __audit_syscall_exit(int success, long return_code)
else
success = AUDITSC_FAILURE;

context = audit_take_context(tsk, success, return_code);
context = audit_take_context(current, success, return_code);
if (!context)
return;

if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
audit_log_exit(context, tsk);
audit_log_exit(context, current);

context->in_syscall = 0;
context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Expand Down Expand Up @@ -1602,7 +1601,7 @@ static inline void handle_one(const struct inode *inode)
int count;
if (likely(!inode->i_fsnotify_marks))
return;
context = current->audit_context;
context = audit_context();
p = context->trees;
count = context->tree_count;
rcu_read_lock();
Expand Down Expand Up @@ -1633,7 +1632,7 @@ static void handle_path(const struct dentry *dentry)
unsigned long seq;
int count;

context = current->audit_context;
context = audit_context();
p = context->trees;
count = context->tree_count;
retry:
Expand Down Expand Up @@ -1715,7 +1714,7 @@ static struct audit_names *audit_alloc_name(struct audit_context *context,
struct filename *
__audit_reusename(const __user char *uptr)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct audit_names *n;

list_for_each_entry(n, &context->names_list, list) {
Expand All @@ -1738,7 +1737,7 @@ __audit_reusename(const __user char *uptr)
*/
void __audit_getname(struct filename *name)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct audit_names *n;

if (!context->in_syscall)
Expand Down Expand Up @@ -1766,7 +1765,7 @@ void __audit_getname(struct filename *name)
void __audit_inode(struct filename *name, const struct dentry *dentry,
unsigned int flags)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct inode *inode = d_backing_inode(dentry);
struct audit_names *n;
bool parent = flags & AUDIT_INODE_PARENT;
Expand Down Expand Up @@ -1865,7 +1864,7 @@ void __audit_inode_child(struct inode *parent,
const struct dentry *dentry,
const unsigned char type)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct inode *inode = d_backing_inode(dentry);
const char *dname = dentry->d_name.name;
struct audit_names *n, *found_parent = NULL, *found_child = NULL;
Expand Down Expand Up @@ -2084,7 +2083,7 @@ int audit_set_loginuid(kuid_t loginuid)
*/
void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

if (attr)
memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
Expand All @@ -2108,7 +2107,7 @@ void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
const struct timespec64 *abs_timeout)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct timespec64 *p = &context->mq_sendrecv.abs_timeout;

if (abs_timeout)
Expand All @@ -2132,7 +2131,7 @@ void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,

void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

if (notification)
context->mq_notify.sigev_signo = notification->sigev_signo;
Expand All @@ -2151,7 +2150,7 @@ void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
*/
void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
context->mq_getsetattr.mqdes = mqdes;
context->mq_getsetattr.mqstat = *mqstat;
context->type = AUDIT_MQ_GETSETATTR;
Expand All @@ -2164,7 +2163,7 @@ void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
*/
void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
context->ipc.uid = ipcp->uid;
context->ipc.gid = ipcp->gid;
context->ipc.mode = ipcp->mode;
Expand All @@ -2184,7 +2183,7 @@ void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
*/
void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

context->ipc.qbytes = qbytes;
context->ipc.perm_uid = uid;
Expand All @@ -2195,7 +2194,7 @@ void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mo

void __audit_bprm(struct linux_binprm *bprm)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

context->type = AUDIT_EXECVE;
context->execve.argc = bprm->argc;
Expand All @@ -2210,7 +2209,7 @@ void __audit_bprm(struct linux_binprm *bprm)
*/
int __audit_socketcall(int nargs, unsigned long *args)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
return -EINVAL;
Expand All @@ -2228,7 +2227,7 @@ int __audit_socketcall(int nargs, unsigned long *args)
*/
void __audit_fd_pair(int fd1, int fd2)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
context->fds[0] = fd1;
context->fds[1] = fd2;
}
Expand All @@ -2242,7 +2241,7 @@ void __audit_fd_pair(int fd1, int fd2)
*/
int __audit_sockaddr(int len, void *a)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

if (!context->sockaddr) {
void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
Expand All @@ -2258,7 +2257,7 @@ int __audit_sockaddr(int len, void *a)

void __audit_ptrace(struct task_struct *t)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

context->target_pid = task_tgid_nr(t);
context->target_auid = audit_get_loginuid(t);
Expand All @@ -2279,19 +2278,18 @@ void __audit_ptrace(struct task_struct *t)
int audit_signal_info(int sig, struct task_struct *t)
{
struct audit_aux_data_pids *axp;
struct task_struct *tsk = current;
struct audit_context *ctx = tsk->audit_context;
struct audit_context *ctx = audit_context();
kuid_t uid = current_uid(), t_uid = task_uid(t);

if (auditd_test_task(t) &&
(sig == SIGTERM || sig == SIGHUP ||
sig == SIGUSR1 || sig == SIGUSR2)) {
audit_sig_pid = task_tgid_nr(tsk);
if (uid_valid(tsk->loginuid))
audit_sig_uid = tsk->loginuid;
audit_sig_pid = task_tgid_nr(current);
if (uid_valid(current->loginuid))
audit_sig_uid = current->loginuid;
else
audit_sig_uid = uid;
security_task_getsecid(tsk, &audit_sig_sid);
security_task_getsecid(current, &audit_sig_sid);
}

if (!audit_signals || audit_dummy_context())
Expand Down Expand Up @@ -2347,7 +2345,7 @@ int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
const struct cred *new, const struct cred *old)
{
struct audit_aux_data_bprm_fcaps *ax;
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
struct cpu_vfs_cap_data vcaps;

ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Expand Down Expand Up @@ -2387,7 +2385,7 @@ int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
*/
void __audit_log_capset(const struct cred *new, const struct cred *old)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
context->capset.pid = task_tgid_nr(current);
context->capset.cap.effective = new->cap_effective;
context->capset.cap.inheritable = new->cap_effective;
Expand All @@ -2398,15 +2396,15 @@ void __audit_log_capset(const struct cred *new, const struct cred *old)

void __audit_mmap_fd(int fd, int flags)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();
context->mmap.fd = fd;
context->mmap.flags = flags;
context->type = AUDIT_MMAP;
}

void __audit_log_kern_module(char *name)
{
struct audit_context *context = current->audit_context;
struct audit_context *context = audit_context();

context->module.name = kmalloc(strlen(name) + 1, GFP_KERNEL);
strcpy(context->module.name, name);
Expand All @@ -2415,7 +2413,7 @@ void __audit_log_kern_module(char *name)

void __audit_fanotify(unsigned int response)
{
audit_log(current->audit_context, GFP_KERNEL,
audit_log(audit_context(), GFP_KERNEL,
AUDIT_FANOTIFY, "resp=%u", response);
}

Expand Down Expand Up @@ -2514,7 +2512,7 @@ void audit_seccomp_actions_logged(const char *names, const char *old_names,

struct list_head *audit_killed_trees(void)
{
struct audit_context *ctx = current->audit_context;
struct audit_context *ctx = audit_context();
if (likely(!ctx || !ctx->in_syscall))
return NULL;
return &ctx->killed_trees;
Expand Down
2 changes: 1 addition & 1 deletion net/bridge/netfilter/ebtables.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ static int do_replace_finish(struct net *net, struct ebt_replace *repl,

#ifdef CONFIG_AUDIT
if (audit_enabled) {
audit_log(current->audit_context, GFP_KERNEL,
audit_log(audit_context(), GFP_KERNEL,
AUDIT_NETFILTER_CFG,
"table=%s family=%u entries=%u",
repl->name, AF_BRIDGE, repl->nentries);
Expand Down
18 changes: 9 additions & 9 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -6749,15 +6749,15 @@ static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
dev->flags & IFF_PROMISC ? "entered" : "left");
if (audit_enabled) {
current_uid_gid(&uid, &gid);
audit_log(current->audit_context, GFP_ATOMIC,
AUDIT_ANOM_PROMISCUOUS,
"dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
dev->name, (dev->flags & IFF_PROMISC),
(old_flags & IFF_PROMISC),
from_kuid(&init_user_ns, audit_get_loginuid(current)),
from_kuid(&init_user_ns, uid),
from_kgid(&init_user_ns, gid),
audit_get_sessionid(current));
audit_log(audit_context(), GFP_ATOMIC,
AUDIT_ANOM_PROMISCUOUS,
"dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
dev->name, (dev->flags & IFF_PROMISC),
(old_flags & IFF_PROMISC),
from_kuid(&init_user_ns, audit_get_loginuid(current)),
from_kuid(&init_user_ns, uid),
from_kgid(&init_user_ns, gid),
audit_get_sessionid(current));
}

dev_change_rx_flags(dev, IFF_PROMISC);
Expand Down
2 changes: 1 addition & 1 deletion net/netfilter/x_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ xt_replace_table(struct xt_table *table,

#ifdef CONFIG_AUDIT
if (audit_enabled) {
audit_log(current->audit_context, GFP_KERNEL,
audit_log(audit_context(), GFP_KERNEL,
AUDIT_NETFILTER_CFG,
"table=%s family=%u entries=%u",
table->name, table->af, private->number);
Expand Down
2 changes: 1 addition & 1 deletion net/netlabel/netlabel_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct audit_buffer *netlbl_audit_start_common(int type,
if (audit_enabled == 0)
return NULL;

audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
audit_buf = audit_log_start(audit_context(), GFP_ATOMIC, type);
if (audit_buf == NULL)
return NULL;

Expand Down
Loading

0 comments on commit cdfb6b3

Please sign in to comment.