Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/kernel/compat.c
0004  *
0005  *  Kernel compatibililty routines for e.g. 32 bit syscall support
0006  *  on 64 bit kernels.
0007  *
0008  *  Copyright (C) 2002-2003 Stephen Rothwell, IBM Corporation
0009  */
0010 
0011 #include <linux/linkage.h>
0012 #include <linux/compat.h>
0013 #include <linux/errno.h>
0014 #include <linux/time.h>
0015 #include <linux/signal.h>
0016 #include <linux/sched.h>    /* for MAX_SCHEDULE_TIMEOUT */
0017 #include <linux/syscalls.h>
0018 #include <linux/unistd.h>
0019 #include <linux/security.h>
0020 #include <linux/export.h>
0021 #include <linux/migrate.h>
0022 #include <linux/posix-timers.h>
0023 #include <linux/times.h>
0024 #include <linux/ptrace.h>
0025 #include <linux/gfp.h>
0026 
0027 #include <linux/uaccess.h>
0028 
0029 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
0030 
0031 /*
0032  * sys_sigprocmask SIG_SETMASK sets the first (compat) word of the
0033  * blocked set of signals to the supplied signal set
0034  */
0035 static inline void compat_sig_setmask(sigset_t *blocked, compat_sigset_word set)
0036 {
0037     memcpy(blocked->sig, &set, sizeof(set));
0038 }
0039 
0040 COMPAT_SYSCALL_DEFINE3(sigprocmask, int, how,
0041                compat_old_sigset_t __user *, nset,
0042                compat_old_sigset_t __user *, oset)
0043 {
0044     old_sigset_t old_set, new_set;
0045     sigset_t new_blocked;
0046 
0047     old_set = current->blocked.sig[0];
0048 
0049     if (nset) {
0050         if (get_user(new_set, nset))
0051             return -EFAULT;
0052         new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
0053 
0054         new_blocked = current->blocked;
0055 
0056         switch (how) {
0057         case SIG_BLOCK:
0058             sigaddsetmask(&new_blocked, new_set);
0059             break;
0060         case SIG_UNBLOCK:
0061             sigdelsetmask(&new_blocked, new_set);
0062             break;
0063         case SIG_SETMASK:
0064             compat_sig_setmask(&new_blocked, new_set);
0065             break;
0066         default:
0067             return -EINVAL;
0068         }
0069 
0070         set_current_blocked(&new_blocked);
0071     }
0072 
0073     if (oset) {
0074         if (put_user(old_set, oset))
0075             return -EFAULT;
0076     }
0077 
0078     return 0;
0079 }
0080 
0081 #endif
0082 
0083 int put_compat_rusage(const struct rusage *r, struct compat_rusage __user *ru)
0084 {
0085     struct compat_rusage r32;
0086     memset(&r32, 0, sizeof(r32));
0087     r32.ru_utime.tv_sec = r->ru_utime.tv_sec;
0088     r32.ru_utime.tv_usec = r->ru_utime.tv_usec;
0089     r32.ru_stime.tv_sec = r->ru_stime.tv_sec;
0090     r32.ru_stime.tv_usec = r->ru_stime.tv_usec;
0091     r32.ru_maxrss = r->ru_maxrss;
0092     r32.ru_ixrss = r->ru_ixrss;
0093     r32.ru_idrss = r->ru_idrss;
0094     r32.ru_isrss = r->ru_isrss;
0095     r32.ru_minflt = r->ru_minflt;
0096     r32.ru_majflt = r->ru_majflt;
0097     r32.ru_nswap = r->ru_nswap;
0098     r32.ru_inblock = r->ru_inblock;
0099     r32.ru_oublock = r->ru_oublock;
0100     r32.ru_msgsnd = r->ru_msgsnd;
0101     r32.ru_msgrcv = r->ru_msgrcv;
0102     r32.ru_nsignals = r->ru_nsignals;
0103     r32.ru_nvcsw = r->ru_nvcsw;
0104     r32.ru_nivcsw = r->ru_nivcsw;
0105     if (copy_to_user(ru, &r32, sizeof(r32)))
0106         return -EFAULT;
0107     return 0;
0108 }
0109 
0110 static int compat_get_user_cpu_mask(compat_ulong_t __user *user_mask_ptr,
0111                     unsigned len, struct cpumask *new_mask)
0112 {
0113     unsigned long *k;
0114 
0115     if (len < cpumask_size())
0116         memset(new_mask, 0, cpumask_size());
0117     else if (len > cpumask_size())
0118         len = cpumask_size();
0119 
0120     k = cpumask_bits(new_mask);
0121     return compat_get_bitmap(k, user_mask_ptr, len * 8);
0122 }
0123 
0124 COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,
0125                unsigned int, len,
0126                compat_ulong_t __user *, user_mask_ptr)
0127 {
0128     cpumask_var_t new_mask;
0129     int retval;
0130 
0131     if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
0132         return -ENOMEM;
0133 
0134     retval = compat_get_user_cpu_mask(user_mask_ptr, len, new_mask);
0135     if (retval)
0136         goto out;
0137 
0138     retval = sched_setaffinity(pid, new_mask);
0139 out:
0140     free_cpumask_var(new_mask);
0141     return retval;
0142 }
0143 
0144 COMPAT_SYSCALL_DEFINE3(sched_getaffinity, compat_pid_t,  pid, unsigned int, len,
0145                compat_ulong_t __user *, user_mask_ptr)
0146 {
0147     int ret;
0148     cpumask_var_t mask;
0149 
0150     if ((len * BITS_PER_BYTE) < nr_cpu_ids)
0151         return -EINVAL;
0152     if (len & (sizeof(compat_ulong_t)-1))
0153         return -EINVAL;
0154 
0155     if (!alloc_cpumask_var(&mask, GFP_KERNEL))
0156         return -ENOMEM;
0157 
0158     ret = sched_getaffinity(pid, mask);
0159     if (ret == 0) {
0160         unsigned int retlen = min(len, cpumask_size());
0161 
0162         if (compat_put_bitmap(user_mask_ptr, cpumask_bits(mask), retlen * 8))
0163             ret = -EFAULT;
0164         else
0165             ret = retlen;
0166     }
0167     free_cpumask_var(mask);
0168 
0169     return ret;
0170 }
0171 
0172 /*
0173  * We currently only need the following fields from the sigevent
0174  * structure: sigev_value, sigev_signo, sig_notify and (sometimes
0175  * sigev_notify_thread_id).  The others are handled in user mode.
0176  * We also assume that copying sigev_value.sival_int is sufficient
0177  * to keep all the bits of sigev_value.sival_ptr intact.
0178  */
0179 int get_compat_sigevent(struct sigevent *event,
0180         const struct compat_sigevent __user *u_event)
0181 {
0182     memset(event, 0, sizeof(*event));
0183     return (!access_ok(u_event, sizeof(*u_event)) ||
0184         __get_user(event->sigev_value.sival_int,
0185             &u_event->sigev_value.sival_int) ||
0186         __get_user(event->sigev_signo, &u_event->sigev_signo) ||
0187         __get_user(event->sigev_notify, &u_event->sigev_notify) ||
0188         __get_user(event->sigev_notify_thread_id,
0189             &u_event->sigev_notify_thread_id))
0190         ? -EFAULT : 0;
0191 }
0192 
0193 long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
0194                unsigned long bitmap_size)
0195 {
0196     unsigned long nr_compat_longs;
0197 
0198     /* align bitmap up to nearest compat_long_t boundary */
0199     bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
0200     nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
0201 
0202     if (!user_read_access_begin(umask, bitmap_size / 8))
0203         return -EFAULT;
0204 
0205     while (nr_compat_longs > 1) {
0206         compat_ulong_t l1, l2;
0207         unsafe_get_user(l1, umask++, Efault);
0208         unsafe_get_user(l2, umask++, Efault);
0209         *mask++ = ((unsigned long)l2 << BITS_PER_COMPAT_LONG) | l1;
0210         nr_compat_longs -= 2;
0211     }
0212     if (nr_compat_longs)
0213         unsafe_get_user(*mask, umask++, Efault);
0214     user_read_access_end();
0215     return 0;
0216 
0217 Efault:
0218     user_read_access_end();
0219     return -EFAULT;
0220 }
0221 
0222 long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
0223                unsigned long bitmap_size)
0224 {
0225     unsigned long nr_compat_longs;
0226 
0227     /* align bitmap up to nearest compat_long_t boundary */
0228     bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
0229     nr_compat_longs = BITS_TO_COMPAT_LONGS(bitmap_size);
0230 
0231     if (!user_write_access_begin(umask, bitmap_size / 8))
0232         return -EFAULT;
0233 
0234     while (nr_compat_longs > 1) {
0235         unsigned long m = *mask++;
0236         unsafe_put_user((compat_ulong_t)m, umask++, Efault);
0237         unsafe_put_user(m >> BITS_PER_COMPAT_LONG, umask++, Efault);
0238         nr_compat_longs -= 2;
0239     }
0240     if (nr_compat_longs)
0241         unsafe_put_user((compat_ulong_t)*mask, umask++, Efault);
0242     user_write_access_end();
0243     return 0;
0244 Efault:
0245     user_write_access_end();
0246     return -EFAULT;
0247 }
0248 
0249 int
0250 get_compat_sigset(sigset_t *set, const compat_sigset_t __user *compat)
0251 {
0252 #ifdef __BIG_ENDIAN
0253     compat_sigset_t v;
0254     if (copy_from_user(&v, compat, sizeof(compat_sigset_t)))
0255         return -EFAULT;
0256     switch (_NSIG_WORDS) {
0257     case 4: set->sig[3] = v.sig[6] | (((long)v.sig[7]) << 32 );
0258         fallthrough;
0259     case 3: set->sig[2] = v.sig[4] | (((long)v.sig[5]) << 32 );
0260         fallthrough;
0261     case 2: set->sig[1] = v.sig[2] | (((long)v.sig[3]) << 32 );
0262         fallthrough;
0263     case 1: set->sig[0] = v.sig[0] | (((long)v.sig[1]) << 32 );
0264     }
0265 #else
0266     if (copy_from_user(set, compat, sizeof(compat_sigset_t)))
0267         return -EFAULT;
0268 #endif
0269     return 0;
0270 }
0271 EXPORT_SYMBOL_GPL(get_compat_sigset);