Skip to content

Commit e1fd1f4

Browse files
author
Al Viro
committed
get rid of union semop in sys_semctl(2) arguments
just have the bugger take unsigned long and deal with SETVAL case (when we use an int member in the union) explicitly. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 4b377ba commit e1fd1f4

7 files changed

Lines changed: 91 additions & 71 deletions

File tree

‎arch/parisc/kernel/sys_parisc32.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,6 @@ asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
6060
return -ENOSYS;
6161
}
6262

63-
asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
64-
{
65-
union semun u;
66-
67-
if (cmd == SETVAL) {
68-
/* Ugh. arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
69-
* The int should be in the first 4, but our argument
70-
* frobbing has left it in the last 4.
71-
*/
72-
u.val = *((int *)&arg + 1);
73-
return sys_semctl (semid, semnum, cmd, u);
74-
}
75-
return sys_semctl (semid, semnum, cmd, arg);
76-
}
77-
7863
asmlinkage long compat_sys_fanotify_mark(int fan_fd, int flags, u32 mask_hi,
7964
u32 mask_lo, int fd,
8065
const char __user *pathname)

‎arch/parisc/kernel/syscall_table.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
ENTRY_COMP(recvmsg)
283283
ENTRY_SAME(semop) /* 185 */
284284
ENTRY_SAME(semget)
285-
ENTRY_DIFF(semctl)
285+
ENTRY_COMP(semctl)
286286
ENTRY_COMP(msgsnd)
287287
ENTRY_COMP(msgrcv)
288288
ENTRY_SAME(msgget) /* 190 */

‎arch/sparc/kernel/sys_sparc_64.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ SYSCALL_DEFINE6(sparc_ipc, unsigned int, call, int, first, unsigned long, second
353353
case SEMCTL: {
354354
err = sys_semctl(first, second,
355355
(int)third | IPC_64,
356-
(union semun) ptr);
356+
(unsigned long) ptr);
357357
goto out;
358358
}
359359
default:

‎include/linux/syscalls.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf);
657657
asmlinkage long sys_semget(key_t key, int nsems, int semflg);
658658
asmlinkage long sys_semop(int semid, struct sembuf __user *sops,
659659
unsigned nsops);
660-
asmlinkage long sys_semctl(int semid, int semnum, int cmd, union semun arg);
660+
asmlinkage long sys_semctl(int semid, int semnum, int cmd, unsigned long arg);
661661
asmlinkage long sys_semtimedop(int semid, struct sembuf __user *sops,
662662
unsigned nsops,
663663
const struct timespec __user *timeout);

‎ipc/compat.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static inline int put_compat_semid_ds(struct semid64_ds *s,
240240

241241
static long do_compat_semctl(int first, int second, int third, u32 pad)
242242
{
243-
union semun fourth;
243+
unsigned long fourth;
244244
int err, err2;
245245
struct semid64_ds s64;
246246
struct semid64_ds __user *up64;
@@ -249,9 +249,13 @@ static long do_compat_semctl(int first, int second, int third, u32 pad)
249249
memset(&s64, 0, sizeof(s64));
250250

251251
if ((third & (~IPC_64)) == SETVAL)
252-
fourth.val = (int) pad;
252+
#ifdef __BIG_ENDIAN
253+
fourth = (unsigned long)pad << 32;
254+
#else
255+
fourth = pad;
256+
#endif
253257
else
254-
fourth.__pad = compat_ptr(pad);
258+
fourth = (unsigned long)compat_ptr(pad);
255259
switch (third & (~IPC_64)) {
256260
case IPC_INFO:
257261
case IPC_RMID:
@@ -269,7 +273,7 @@ static long do_compat_semctl(int first, int second, int third, u32 pad)
269273
case IPC_STAT:
270274
case SEM_STAT:
271275
up64 = compat_alloc_user_space(sizeof(s64));
272-
fourth.__pad = up64;
276+
fourth = (unsigned long)up64;
273277
err = sys_semctl(first, second, third, fourth);
274278
if (err < 0)
275279
break;
@@ -295,7 +299,7 @@ static long do_compat_semctl(int first, int second, int third, u32 pad)
295299
if (err)
296300
break;
297301

298-
fourth.__pad = up64;
302+
fourth = (unsigned long)up64;
299303
err = sys_semctl(first, second, third, fourth);
300304
break;
301305

‎ipc/sem.c‎

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in,
799799
}
800800

801801
static int semctl_nolock(struct ipc_namespace *ns, int semid,
802-
int cmd, int version, union semun arg)
802+
int cmd, int version, void __user *p)
803803
{
804804
int err;
805805
struct sem_array *sma;
@@ -834,7 +834,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
834834
}
835835
max_id = ipc_get_maxid(&sem_ids(ns));
836836
up_read(&sem_ids(ns).rw_mutex);
837-
if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
837+
if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
838838
return -EFAULT;
839839
return (max_id < 0) ? 0: max_id;
840840
}
@@ -871,7 +871,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
871871
tbuf.sem_ctime = sma->sem_ctime;
872872
tbuf.sem_nsems = sma->sem_nsems;
873873
sem_unlock(sma);
874-
if (copy_semid_to_user (arg.buf, &tbuf, version))
874+
if (copy_semid_to_user(p, &tbuf, version))
875875
return -EFAULT;
876876
return id;
877877
}
@@ -883,8 +883,67 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
883883
return err;
884884
}
885885

886+
static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
887+
unsigned long arg)
888+
{
889+
struct sem_undo *un;
890+
struct sem_array *sma;
891+
struct sem* curr;
892+
int err;
893+
int nsems;
894+
struct list_head tasks;
895+
int val;
896+
#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
897+
/* big-endian 64bit */
898+
val = arg >> 32;
899+
#else
900+
/* 32bit or little-endian 64bit */
901+
val = arg;
902+
#endif
903+
904+
sma = sem_lock_check(ns, semid);
905+
if (IS_ERR(sma))
906+
return PTR_ERR(sma);
907+
908+
INIT_LIST_HEAD(&tasks);
909+
nsems = sma->sem_nsems;
910+
911+
err = -EACCES;
912+
if (ipcperms(ns, &sma->sem_perm, S_IWUGO))
913+
goto out_unlock;
914+
915+
err = security_sem_semctl(sma, SETVAL);
916+
if (err)
917+
goto out_unlock;
918+
919+
err = -EINVAL;
920+
if(semnum < 0 || semnum >= nsems)
921+
goto out_unlock;
922+
923+
curr = &sma->sem_base[semnum];
924+
925+
err = -ERANGE;
926+
if (val > SEMVMX || val < 0)
927+
goto out_unlock;
928+
929+
assert_spin_locked(&sma->sem_perm.lock);
930+
list_for_each_entry(un, &sma->list_id, list_id)
931+
un->semadj[semnum] = 0;
932+
933+
curr->semval = val;
934+
curr->sempid = task_tgid_vnr(current);
935+
sma->sem_ctime = get_seconds();
936+
/* maybe some queued-up processes were waiting for this */
937+
do_smart_update(sma, NULL, 0, 0, &tasks);
938+
err = 0;
939+
out_unlock:
940+
sem_unlock(sma);
941+
wake_up_sem_queue_do(&tasks);
942+
return err;
943+
}
944+
886945
static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
887-
int cmd, int version, union semun arg)
946+
int cmd, void __user *p)
888947
{
889948
struct sem_array *sma;
890949
struct sem* curr;
@@ -903,7 +962,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
903962

904963
err = -EACCES;
905964
if (ipcperms(ns, &sma->sem_perm,
906-
(cmd == SETVAL || cmd == SETALL) ? S_IWUGO : S_IRUGO))
965+
cmd == SETALL ? S_IWUGO : S_IRUGO))
907966
goto out_unlock;
908967

909968
err = security_sem_semctl(sma, cmd);
@@ -914,7 +973,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
914973
switch (cmd) {
915974
case GETALL:
916975
{
917-
ushort __user *array = arg.array;
976+
ushort __user *array = p;
918977
int i;
919978

920979
if(nsems > SEMMSL_FAST) {
@@ -957,7 +1016,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
9571016
}
9581017
}
9591018

960-
if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
1019+
if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
9611020
sem_putref(sma);
9621021
err = -EFAULT;
9631022
goto out_free;
@@ -991,7 +1050,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
9911050
err = 0;
9921051
goto out_unlock;
9931052
}
994-
/* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
1053+
/* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
9951054
}
9961055
err = -EINVAL;
9971056
if(semnum < 0 || semnum >= nsems)
@@ -1012,27 +1071,6 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
10121071
case GETZCNT:
10131072
err = count_semzcnt(sma,semnum);
10141073
goto out_unlock;
1015-
case SETVAL:
1016-
{
1017-
int val = arg.val;
1018-
struct sem_undo *un;
1019-
1020-
err = -ERANGE;
1021-
if (val > SEMVMX || val < 0)
1022-
goto out_unlock;
1023-
1024-
assert_spin_locked(&sma->sem_perm.lock);
1025-
list_for_each_entry(un, &sma->list_id, list_id)
1026-
un->semadj[semnum] = 0;
1027-
1028-
curr->semval = val;
1029-
curr->sempid = task_tgid_vnr(current);
1030-
sma->sem_ctime = get_seconds();
1031-
/* maybe some queued-up processes were waiting for this */
1032-
do_smart_update(sma, NULL, 0, 0, &tasks);
1033-
err = 0;
1034-
goto out_unlock;
1035-
}
10361074
}
10371075
out_unlock:
10381076
sem_unlock(sma);
@@ -1076,15 +1114,15 @@ copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
10761114
* NOTE: no locks must be held, the rw_mutex is taken inside this function.
10771115
*/
10781116
static int semctl_down(struct ipc_namespace *ns, int semid,
1079-
int cmd, int version, union semun arg)
1117+
int cmd, int version, void __user *p)
10801118
{
10811119
struct sem_array *sma;
10821120
int err;
10831121
struct semid64_ds semid64;
10841122
struct kern_ipc_perm *ipcp;
10851123

10861124
if(cmd == IPC_SET) {
1087-
if (copy_semid_from_user(&semid64, arg.buf, version))
1125+
if (copy_semid_from_user(&semid64, p, version))
10881126
return -EFAULT;
10891127
}
10901128

@@ -1120,11 +1158,11 @@ static int semctl_down(struct ipc_namespace *ns, int semid,
11201158
return err;
11211159
}
11221160

1123-
SYSCALL_DEFINE(semctl)(int semid, int semnum, int cmd, union semun arg)
1161+
SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
11241162
{
1125-
int err = -EINVAL;
11261163
int version;
11271164
struct ipc_namespace *ns;
1165+
void __user *p = (void __user *)arg;
11281166

11291167
if (semid < 0)
11301168
return -EINVAL;
@@ -1137,30 +1175,23 @@ SYSCALL_DEFINE(semctl)(int semid, int semnum, int cmd, union semun arg)
11371175
case SEM_INFO:
11381176
case IPC_STAT:
11391177
case SEM_STAT:
1140-
err = semctl_nolock(ns, semid, cmd, version, arg);
1141-
return err;
1178+
return semctl_nolock(ns, semid, cmd, version, p);
11421179
case GETALL:
11431180
case GETVAL:
11441181
case GETPID:
11451182
case GETNCNT:
11461183
case GETZCNT:
1147-
case SETVAL:
11481184
case SETALL:
1149-
err = semctl_main(ns,semid,semnum,cmd,version,arg);
1150-
return err;
1185+
return semctl_main(ns, semid, semnum, cmd, p);
1186+
case SETVAL:
1187+
return semctl_setval(ns, semid, semnum, arg);
11511188
case IPC_RMID:
11521189
case IPC_SET:
1153-
err = semctl_down(ns, semid, cmd, version, arg);
1154-
return err;
1190+
return semctl_down(ns, semid, cmd, version, p);
11551191
default:
11561192
return -EINVAL;
11571193
}
11581194
}
1159-
asmlinkage long SyS_semctl(int semid, int semnum, int cmd, union semun arg)
1160-
{
1161-
return SYSC_semctl((int) semid, (int) semnum, (int) cmd, arg);
1162-
}
1163-
SYSCALL_ALIAS(sys_semctl, SyS_semctl);
11641195

11651196
/* If the task doesn't already have a undo_list, then allocate one
11661197
* here. We guarantee there is only one thread using this undo list,

‎ipc/syscall.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
3333
case SEMGET:
3434
return sys_semget(first, second, third);
3535
case SEMCTL: {
36-
union semun fourth;
36+
unsigned long arg;
3737
if (!ptr)
3838
return -EINVAL;
39-
if (get_user(fourth.__pad, (void __user * __user *) ptr))
39+
if (get_user(arg, (unsigned long __user *) ptr))
4040
return -EFAULT;
41-
return sys_semctl(first, second, third, fourth);
41+
return sys_semctl(first, second, third, arg);
4242
}
4343

4444
case MSGSND:

0 commit comments

Comments
 (0)