Skip to content

Commit 564f699

Browse files
author
Al Viro
committed
sanitize audit_mq_open()
* don't bother with allocations * don't do double copy_from_user() * don't duplicate parts of check for audit_dummy_context() Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent c32c8af commit 564f699

3 files changed

Lines changed: 38 additions & 59 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ extern void audit_socketcall(int nargs, unsigned long *args);
450450
extern int audit_sockaddr(int len, void *addr);
451451
extern int __audit_fd_pair(int fd1, int fd2);
452452
extern int audit_set_macxattr(const char *name);
453-
extern int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr);
453+
extern void __audit_mq_open(int oflag, mode_t mode, struct mq_attr *attr);
454454
extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
455455
extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
456456
extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
@@ -475,11 +475,10 @@ static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid
475475
if (unlikely(!audit_dummy_context()))
476476
__audit_ipc_set_perm(qbytes, uid, gid, mode);
477477
}
478-
static inline int audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
478+
static inline void audit_mq_open(int oflag, mode_t mode, struct mq_attr *attr)
479479
{
480480
if (unlikely(!audit_dummy_context()))
481-
return __audit_mq_open(oflag, mode, u_attr);
482-
return 0;
481+
__audit_mq_open(oflag, mode, attr);
483482
}
484483
static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout)
485484
{
@@ -541,7 +540,7 @@ extern int audit_signals;
541540
#define audit_fd_pair(n,a) ({ 0; })
542541
#define audit_sockaddr(len, addr) ({ 0; })
543542
#define audit_set_macxattr(n) do { ; } while (0)
544-
#define audit_mq_open(o,m,a) ({ 0; })
543+
#define audit_mq_open(o,m,a) ((void)0)
545544
#define audit_mq_sendrecv(d,l,p,t) ((void)0)
546545
#define audit_mq_notify(d,n) ((void)0)
547546
#define audit_mq_getsetattr(d,s) ((void)0)

‎ipc/mqueue.c‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -588,22 +588,18 @@ static int mq_attr_ok(struct mq_attr *attr)
588588
* Invoked when creating a new queue via sys_mq_open
589589
*/
590590
static struct file *do_create(struct dentry *dir, struct dentry *dentry,
591-
int oflag, mode_t mode, struct mq_attr __user *u_attr)
591+
int oflag, mode_t mode, struct mq_attr *attr)
592592
{
593593
const struct cred *cred = current_cred();
594-
struct mq_attr attr;
595594
struct file *result;
596595
int ret;
597596

598-
if (u_attr) {
599-
ret = -EFAULT;
600-
if (copy_from_user(&attr, u_attr, sizeof(attr)))
601-
goto out;
597+
if (attr) {
602598
ret = -EINVAL;
603-
if (!mq_attr_ok(&attr))
599+
if (!mq_attr_ok(attr))
604600
goto out;
605601
/* store for use during create */
606-
dentry->d_fsdata = &attr;
602+
dentry->d_fsdata = attr;
607603
}
608604

609605
mode &= ~current->fs->umask;
@@ -660,11 +656,13 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
660656
struct dentry *dentry;
661657
struct file *filp;
662658
char *name;
659+
struct mq_attr attr;
663660
int fd, error;
664661

665-
error = audit_mq_open(oflag, mode, u_attr);
666-
if (error != 0)
667-
return error;
662+
if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
663+
return -EFAULT;
664+
665+
audit_mq_open(oflag, mode, u_attr ? &attr : NULL);
668666

669667
if (IS_ERR(name = getname(u_name)))
670668
return PTR_ERR(name);
@@ -690,7 +688,8 @@ asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
690688
filp = do_open(dentry, oflag);
691689
} else {
692690
filp = do_create(mqueue_mnt->mnt_root, dentry,
693-
oflag, mode, u_attr);
691+
oflag, mode,
692+
u_attr ? &attr : NULL);
694693
}
695694
} else {
696695
error = -ENOENT;

‎kernel/auditsc.c‎

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ struct audit_aux_data {
124124
/* Number of target pids per aux struct. */
125125
#define AUDIT_AUX_PIDS 16
126126

127-
struct audit_aux_data_mq_open {
128-
struct audit_aux_data d;
129-
int oflag;
130-
mode_t mode;
131-
struct mq_attr attr;
132-
};
133-
134127
struct audit_aux_data_execve {
135128
struct audit_aux_data d;
136129
int argc;
@@ -242,6 +235,11 @@ struct audit_context {
242235
unsigned int msg_prio;
243236
struct timespec abs_timeout;
244237
} mq_sendrecv;
238+
struct {
239+
int oflag;
240+
mode_t mode;
241+
struct mq_attr attr;
242+
} mq_open;
245243
};
246244

247245
#if AUDIT_DEBUG
@@ -1263,6 +1261,16 @@ static void show_special(struct audit_context *context, int *call_panic)
12631261
return;
12641262
}
12651263
break; }
1264+
case AUDIT_MQ_OPEN: {
1265+
audit_log_format(ab,
1266+
"oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1267+
"mq_msgsize=%ld mq_curmsgs=%ld",
1268+
context->mq_open.oflag, context->mq_open.mode,
1269+
context->mq_open.attr.mq_flags,
1270+
context->mq_open.attr.mq_maxmsg,
1271+
context->mq_open.attr.mq_msgsize,
1272+
context->mq_open.attr.mq_curmsgs);
1273+
break; }
12661274
case AUDIT_MQ_SENDRECV: {
12671275
audit_log_format(ab,
12681276
"mqdes=%d msg_len=%zd msg_prio=%u "
@@ -1368,15 +1376,6 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
13681376
continue; /* audit_panic has been called */
13691377

13701378
switch (aux->type) {
1371-
case AUDIT_MQ_OPEN: {
1372-
struct audit_aux_data_mq_open *axi = (void *)aux;
1373-
audit_log_format(ab,
1374-
"oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1375-
"mq_msgsize=%ld mq_curmsgs=%ld",
1376-
axi->oflag, axi->mode, axi->attr.mq_flags,
1377-
axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1378-
axi->attr.mq_curmsgs);
1379-
break; }
13801379

13811380
case AUDIT_EXECVE: {
13821381
struct audit_aux_data_execve *axi = (void *)aux;
@@ -2135,38 +2134,20 @@ int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
21352134
* @mode: mode bits
21362135
* @u_attr: queue attributes
21372136
*
2138-
* Returns 0 for success or NULL context or < 0 on error.
21392137
*/
2140-
int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2138+
void __audit_mq_open(int oflag, mode_t mode, struct mq_attr *attr)
21412139
{
2142-
struct audit_aux_data_mq_open *ax;
21432140
struct audit_context *context = current->audit_context;
21442141

2145-
if (!audit_enabled)
2146-
return 0;
2147-
2148-
if (likely(!context))
2149-
return 0;
2150-
2151-
ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2152-
if (!ax)
2153-
return -ENOMEM;
2154-
2155-
if (u_attr != NULL) {
2156-
if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2157-
kfree(ax);
2158-
return -EFAULT;
2159-
}
2160-
} else
2161-
memset(&ax->attr, 0, sizeof(ax->attr));
2142+
if (attr)
2143+
memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2144+
else
2145+
memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
21622146

2163-
ax->oflag = oflag;
2164-
ax->mode = mode;
2147+
context->mq_open.oflag = oflag;
2148+
context->mq_open.mode = mode;
21652149

2166-
ax->d.type = AUDIT_MQ_OPEN;
2167-
ax->d.next = context->aux;
2168-
context->aux = (void *)ax;
2169-
return 0;
2150+
context->type = AUDIT_MQ_OPEN;
21702151
}
21712152

21722153
/**

0 commit comments

Comments
 (0)