Skip to content

Commit ded653c

Browse files
deepa-hubarndb
authored andcommitted
signal: Add set_user_sigmask()
Refactor reading sigset from userspace and updating sigmask into an api. This is useful for versions of syscalls that pass in the sigmask and expect the current->sigmask to be changed during, and restored after, the execution of the syscall. With the advent of new y2038 syscalls in the subsequent patches, we add two more new versions of the syscalls (for pselect, ppoll, and io_pgetevents) in addition to the existing native and compat versions. Adding such an api reduces the logic that would need to be replicated otherwise. Note that the calls to sigprocmask() ignored the return value from the api as the function only returns an error on an invalid first argument that is hardcoded at these call sites. The updated logic uses set_current_blocked() instead. Signed-off-by: Deepa Dinamani <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]>
1 parent 6510223 commit ded653c

File tree

6 files changed

+76
-70
lines changed

6 files changed

+76
-70
lines changed

fs/aio.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,14 +2104,10 @@ SYSCALL_DEFINE6(io_pgetevents,
21042104
if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
21052105
return -EFAULT;
21062106

2107-
if (ksig.sigmask) {
2108-
if (ksig.sigsetsize != sizeof(sigset_t))
2109-
return -EINVAL;
2110-
if (copy_from_user(&ksigmask, ksig.sigmask, sizeof(ksigmask)))
2111-
return -EFAULT;
2112-
sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2113-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2114-
}
2107+
2108+
ret = set_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2109+
if (ret)
2110+
return ret;
21152111

21162112
ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &ts : NULL);
21172113
if (signal_pending(current)) {
@@ -2174,14 +2170,9 @@ COMPAT_SYSCALL_DEFINE6(io_pgetevents,
21742170
if (usig && copy_from_user(&ksig, usig, sizeof(ksig)))
21752171
return -EFAULT;
21762172

2177-
if (ksig.sigmask) {
2178-
if (ksig.sigsetsize != sizeof(compat_sigset_t))
2179-
return -EINVAL;
2180-
if (get_compat_sigset(&ksigmask, ksig.sigmask))
2181-
return -EFAULT;
2182-
sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2183-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
2184-
}
2173+
ret = set_compat_user_sigmask(ksig.sigmask, &ksigmask, &sigsaved, ksig.sigsetsize);
2174+
if (ret)
2175+
return ret;
21852176

21862177
ret = do_io_getevents(ctx_id, min_nr, nr, events, timeout ? &t : NULL);
21872178
if (signal_pending(current)) {

fs/eventpoll.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,14 +2223,9 @@ SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
22232223
* If the caller wants a certain signal mask to be set during the wait,
22242224
* we apply it here.
22252225
*/
2226-
if (sigmask) {
2227-
if (sigsetsize != sizeof(sigset_t))
2228-
return -EINVAL;
2229-
if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
2230-
return -EFAULT;
2231-
sigsaved = current->blocked;
2232-
set_current_blocked(&ksigmask);
2233-
}
2226+
error = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
2227+
if (error)
2228+
return error;
22342229

22352230
error = do_epoll_wait(epfd, events, maxevents, timeout);
22362231

@@ -2266,14 +2261,9 @@ COMPAT_SYSCALL_DEFINE6(epoll_pwait, int, epfd,
22662261
* If the caller wants a certain signal mask to be set during the wait,
22672262
* we apply it here.
22682263
*/
2269-
if (sigmask) {
2270-
if (sigsetsize != sizeof(compat_sigset_t))
2271-
return -EINVAL;
2272-
if (get_compat_sigset(&ksigmask, sigmask))
2273-
return -EFAULT;
2274-
sigsaved = current->blocked;
2275-
set_current_blocked(&ksigmask);
2276-
}
2264+
err = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
2265+
if (err)
2266+
return err;
22772267

22782268
err = do_epoll_wait(epfd, events, maxevents, timeout);
22792269

fs/select.c

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -717,16 +717,9 @@ static long do_pselect(int n, fd_set __user *inp, fd_set __user *outp,
717717
return -EINVAL;
718718
}
719719

720-
if (sigmask) {
721-
/* XXX: Don't preclude handling different sized sigset_t's. */
722-
if (sigsetsize != sizeof(sigset_t))
723-
return -EINVAL;
724-
if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
725-
return -EFAULT;
726-
727-
sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
728-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
729-
}
720+
ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
721+
if (ret)
722+
return ret;
730723

731724
ret = core_sys_select(n, inp, outp, exp, to);
732725
ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
@@ -1061,16 +1054,9 @@ SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds, unsigned int, nfds,
10611054
return -EINVAL;
10621055
}
10631056

1064-
if (sigmask) {
1065-
/* XXX: Don't preclude handling different sized sigset_t's. */
1066-
if (sigsetsize != sizeof(sigset_t))
1067-
return -EINVAL;
1068-
if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
1069-
return -EFAULT;
1070-
1071-
sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1072-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1073-
}
1057+
ret = set_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1058+
if (ret)
1059+
return ret;
10741060

10751061
ret = do_sys_poll(ufds, nfds, to);
10761062

@@ -1323,15 +1309,9 @@ static long do_compat_pselect(int n, compat_ulong_t __user *inp,
13231309
return -EINVAL;
13241310
}
13251311

1326-
if (sigmask) {
1327-
if (sigsetsize != sizeof(compat_sigset_t))
1328-
return -EINVAL;
1329-
if (get_compat_sigset(&ksigmask, sigmask))
1330-
return -EFAULT;
1331-
1332-
sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1333-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1334-
}
1312+
ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1313+
if (ret)
1314+
return ret;
13351315

13361316
ret = compat_core_sys_select(n, inp, outp, exp, to);
13371317
ret = compat_poll_select_copy_remaining(&end_time, tsp, 0, ret);
@@ -1389,15 +1369,9 @@ COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
13891369
return -EINVAL;
13901370
}
13911371

1392-
if (sigmask) {
1393-
if (sigsetsize != sizeof(compat_sigset_t))
1394-
return -EINVAL;
1395-
if (get_compat_sigset(&ksigmask, sigmask))
1396-
return -EFAULT;
1397-
1398-
sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1399-
sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1400-
}
1372+
ret = set_compat_user_sigmask(sigmask, &ksigmask, &sigsaved, sigsetsize);
1373+
if (ret)
1374+
return ret;
14011375

14021376
ret = do_sys_poll(ufds, nfds, to);
14031377

include/linux/compat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ typedef struct {
169169
compat_sigset_word sig[_COMPAT_NSIG_WORDS];
170170
} compat_sigset_t;
171171

172+
int set_compat_user_sigmask(const compat_sigset_t __user *usigmask,
173+
sigset_t *set, sigset_t *oldset,
174+
size_t sigsetsize);
175+
172176
struct compat_sigaction {
173177
#ifndef __ARCH_HAS_IRIX_SIGACTION
174178
compat_uptr_t sa_handler;

include/linux/signal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ extern int group_send_sig_info(int sig, struct kernel_siginfo *info,
273273
struct task_struct *p, enum pid_type type);
274274
extern int __group_send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
275275
extern int sigprocmask(int, sigset_t *, sigset_t *);
276+
extern int set_user_sigmask(const sigset_t __user *usigmask, sigset_t *set,
277+
sigset_t *oldset, size_t sigsetsize);
276278
extern void set_current_blocked(sigset_t *);
277279
extern void __set_current_blocked(const sigset_t *);
278280
extern int show_unhandled_signals;

kernel/signal.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,51 @@ int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
27352735
}
27362736
EXPORT_SYMBOL(sigprocmask);
27372737

2738+
/*
2739+
* The api helps set app-provided sigmasks.
2740+
*
2741+
* This is useful for syscalls such as ppoll, pselect, io_pgetevents and
2742+
* epoll_pwait where a new sigmask is passed from userland for the syscalls.
2743+
*/
2744+
int set_user_sigmask(const sigset_t __user *usigmask, sigset_t *set,
2745+
sigset_t *oldset, size_t sigsetsize)
2746+
{
2747+
if (!usigmask)
2748+
return 0;
2749+
2750+
if (sigsetsize != sizeof(sigset_t))
2751+
return -EINVAL;
2752+
if (copy_from_user(set, usigmask, sizeof(sigset_t)))
2753+
return -EFAULT;
2754+
2755+
*oldset = current->blocked;
2756+
set_current_blocked(set);
2757+
2758+
return 0;
2759+
}
2760+
EXPORT_SYMBOL(set_user_sigmask);
2761+
2762+
#ifdef CONFIG_COMPAT
2763+
int set_compat_user_sigmask(const compat_sigset_t __user *usigmask,
2764+
sigset_t *set, sigset_t *oldset,
2765+
size_t sigsetsize)
2766+
{
2767+
if (!usigmask)
2768+
return 0;
2769+
2770+
if (sigsetsize != sizeof(compat_sigset_t))
2771+
return -EINVAL;
2772+
if (get_compat_sigset(set, usigmask))
2773+
return -EFAULT;
2774+
2775+
*oldset = current->blocked;
2776+
set_current_blocked(set);
2777+
2778+
return 0;
2779+
}
2780+
EXPORT_SYMBOL(set_compat_user_sigmask);
2781+
#endif
2782+
27382783
/**
27392784
* sys_rt_sigprocmask - change the list of currently blocked signals
27402785
* @how: whether to add, remove, or set signals

0 commit comments

Comments
 (0)