Skip to content

Commit 651971c

Browse files
suzukiLinus Torvalds
authored andcommitted
[PATCH] Fix the size limit of compat space msgsize
Currently we allocate 64k space on the user stack and use it the msgbuf for sys_{msgrcv,msgsnd} for compat and the results are later copied in user [ by copy_in_user]. This patch introduces helper routines for sys_{msgrcv,msgsnd} as below: do_msgsnd() : Accepts the mtype and user space ptr to the buffer along with the msqid and msgflg. do_msgrcv() : Accepts a kernel space ptr to mtype and a userspace ptr to the buffer. The mtype has to be copied back the user space msgbuf by the caller. These changes avoid the need to allocate the msgsize on the userspace ( thus removing the size limt ) and the overhead of an extra copy_in_user(). Signed-off-by: Suzuki K P <suzuki@in.ibm.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: "David S. Miller" <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 19e5d9c commit 651971c

3 files changed

Lines changed: 46 additions & 27 deletions

File tree

‎include/linux/msg.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ struct msg_queue {
9292
struct list_head q_senders;
9393
};
9494

95+
/* Helper routines for sys_msgsnd and sys_msgrcv */
96+
extern long do_msgsnd(int msqid, long mtype, void __user *mtext,
97+
size_t msgsz, int msgflg);
98+
extern long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
99+
size_t msgsz, long msgtyp, int msgflg);
100+
95101
#endif /* __KERNEL__ */
96102

97103
#endif /* _LINUX_MSG_H */

‎ipc/compat.c‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ struct compat_shm_info {
115115

116116
extern int sem_ctls[];
117117
#define sc_semopm (sem_ctls[2])
118-
#define MAXBUF (64*1024)
119118

120119
static inline int compat_ipc_parse_version(int *cmd)
121120
{
@@ -307,35 +306,30 @@ long compat_sys_semctl(int first, int second, int third, void __user *uptr)
307306

308307
long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
309308
{
310-
struct msgbuf __user *p;
311309
struct compat_msgbuf __user *up = uptr;
312310
long type;
313311

314312
if (first < 0)
315313
return -EINVAL;
316-
if (second < 0 || (second >= MAXBUF - sizeof(struct msgbuf)))
314+
if (second < 0)
317315
return -EINVAL;
318316

319-
p = compat_alloc_user_space(second + sizeof(struct msgbuf));
320-
if (get_user(type, &up->mtype) ||
321-
put_user(type, &p->mtype) ||
322-
copy_in_user(p->mtext, up->mtext, second))
317+
if (get_user(type, &up->mtype))
323318
return -EFAULT;
324319

325-
return sys_msgsnd(first, p, second, third);
320+
return do_msgsnd(first, type, up->mtext, second, third);
326321
}
327322

328323
long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
329324
int version, void __user *uptr)
330325
{
331-
struct msgbuf __user *p;
332326
struct compat_msgbuf __user *up;
333327
long type;
334328
int err;
335329

336330
if (first < 0)
337331
return -EINVAL;
338-
if (second < 0 || (second >= MAXBUF - sizeof(struct msgbuf)))
332+
if (second < 0)
339333
return -EINVAL;
340334

341335
if (!version) {
@@ -349,14 +343,11 @@ long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
349343
uptr = compat_ptr(ipck.msgp);
350344
msgtyp = ipck.msgtyp;
351345
}
352-
p = compat_alloc_user_space(second + sizeof(struct msgbuf));
353-
err = sys_msgrcv(first, p, second, msgtyp, third);
346+
up = uptr;
347+
err = do_msgrcv(first, &type, up->mtext, second, msgtyp, third);
354348
if (err < 0)
355349
goto out;
356-
up = uptr;
357-
if (get_user(type, &p->mtype) ||
358-
put_user(type, &up->mtype) ||
359-
copy_in_user(up->mtext, p->mtext, err))
350+
if (put_user(type, &up->mtype))
360351
err = -EFAULT;
361352
out:
362353
return err;

‎ipc/msg.c‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -626,25 +626,22 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
626626
return 0;
627627
}
628628

629-
asmlinkage long
630-
sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
629+
long do_msgsnd(int msqid, long mtype, void __user *mtext,
630+
size_t msgsz, int msgflg)
631631
{
632632
struct msg_queue *msq;
633633
struct msg_msg *msg;
634-
long mtype;
635634
int err;
636635
struct ipc_namespace *ns;
637636

638637
ns = current->nsproxy->ipc_ns;
639638

640639
if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
641640
return -EINVAL;
642-
if (get_user(mtype, &msgp->mtype))
643-
return -EFAULT;
644641
if (mtype < 1)
645642
return -EINVAL;
646643

647-
msg = load_msg(msgp->mtext, msgsz);
644+
msg = load_msg(mtext, msgsz);
648645
if (IS_ERR(msg))
649646
return PTR_ERR(msg);
650647

@@ -723,6 +720,16 @@ sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
723720
return err;
724721
}
725722

723+
asmlinkage long
724+
sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
725+
{
726+
long mtype;
727+
728+
if (get_user(mtype, &msgp->mtype))
729+
return -EFAULT;
730+
return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
731+
}
732+
726733
static inline int convert_mode(long *msgtyp, int msgflg)
727734
{
728735
/*
@@ -742,8 +749,8 @@ static inline int convert_mode(long *msgtyp, int msgflg)
742749
return SEARCH_EQUAL;
743750
}
744751

745-
asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
746-
long msgtyp, int msgflg)
752+
long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
753+
size_t msgsz, long msgtyp, int msgflg)
747754
{
748755
struct msg_queue *msq;
749756
struct msg_msg *msg;
@@ -889,15 +896,30 @@ asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
889896
return PTR_ERR(msg);
890897

891898
msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
892-
if (put_user (msg->m_type, &msgp->mtype) ||
893-
store_msg(msgp->mtext, msg, msgsz)) {
899+
*pmtype = msg->m_type;
900+
if (store_msg(mtext, msg, msgsz))
894901
msgsz = -EFAULT;
895-
}
902+
896903
free_msg(msg);
897904

898905
return msgsz;
899906
}
900907

908+
asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
909+
long msgtyp, int msgflg)
910+
{
911+
long err, mtype;
912+
913+
err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
914+
if (err < 0)
915+
goto out;
916+
917+
if (put_user(mtype, &msgp->mtype))
918+
err = -EFAULT;
919+
out:
920+
return err;
921+
}
922+
901923
#ifdef CONFIG_PROC_FS
902924
static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
903925
{

0 commit comments

Comments
 (0)