Back to home page

LXR

 
 

    


0001 #include <linux/kernel.h>
0002 #include <linux/syscalls.h>
0003 #include <linux/fdtable.h>
0004 #include <linux/string.h>
0005 #include <linux/random.h>
0006 #include <linux/module.h>
0007 #include <linux/ptrace.h>
0008 #include <linux/init.h>
0009 #include <linux/errno.h>
0010 #include <linux/cache.h>
0011 #include <linux/bug.h>
0012 #include <linux/err.h>
0013 #include <linux/kcmp.h>
0014 
0015 #include <asm/unistd.h>
0016 
0017 /*
0018  * We don't expose the real in-memory order of objects for security reasons.
0019  * But still the comparison results should be suitable for sorting. So we
0020  * obfuscate kernel pointers values and compare the production instead.
0021  *
0022  * The obfuscation is done in two steps. First we xor the kernel pointer with
0023  * a random value, which puts pointer into a new position in a reordered space.
0024  * Secondly we multiply the xor production with a large odd random number to
0025  * permute its bits even more (the odd multiplier guarantees that the product
0026  * is unique ever after the high bits are truncated, since any odd number is
0027  * relative prime to 2^n).
0028  *
0029  * Note also that the obfuscation itself is invisible to userspace and if needed
0030  * it can be changed to an alternate scheme.
0031  */
0032 static unsigned long cookies[KCMP_TYPES][2] __read_mostly;
0033 
0034 static long kptr_obfuscate(long v, int type)
0035 {
0036     return (v ^ cookies[type][0]) * cookies[type][1];
0037 }
0038 
0039 /*
0040  * 0 - equal, i.e. v1 = v2
0041  * 1 - less than, i.e. v1 < v2
0042  * 2 - greater than, i.e. v1 > v2
0043  * 3 - not equal but ordering unavailable (reserved for future)
0044  */
0045 static int kcmp_ptr(void *v1, void *v2, enum kcmp_type type)
0046 {
0047     long t1, t2;
0048 
0049     t1 = kptr_obfuscate((long)v1, type);
0050     t2 = kptr_obfuscate((long)v2, type);
0051 
0052     return (t1 < t2) | ((t1 > t2) << 1);
0053 }
0054 
0055 /* The caller must have pinned the task */
0056 static struct file *
0057 get_file_raw_ptr(struct task_struct *task, unsigned int idx)
0058 {
0059     struct file *file = NULL;
0060 
0061     task_lock(task);
0062     rcu_read_lock();
0063 
0064     if (task->files)
0065         file = fcheck_files(task->files, idx);
0066 
0067     rcu_read_unlock();
0068     task_unlock(task);
0069 
0070     return file;
0071 }
0072 
0073 static void kcmp_unlock(struct mutex *m1, struct mutex *m2)
0074 {
0075     if (likely(m2 != m1))
0076         mutex_unlock(m2);
0077     mutex_unlock(m1);
0078 }
0079 
0080 static int kcmp_lock(struct mutex *m1, struct mutex *m2)
0081 {
0082     int err;
0083 
0084     if (m2 > m1)
0085         swap(m1, m2);
0086 
0087     err = mutex_lock_killable(m1);
0088     if (!err && likely(m1 != m2)) {
0089         err = mutex_lock_killable_nested(m2, SINGLE_DEPTH_NESTING);
0090         if (err)
0091             mutex_unlock(m1);
0092     }
0093 
0094     return err;
0095 }
0096 
0097 SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
0098         unsigned long, idx1, unsigned long, idx2)
0099 {
0100     struct task_struct *task1, *task2;
0101     int ret;
0102 
0103     rcu_read_lock();
0104 
0105     /*
0106      * Tasks are looked up in caller's PID namespace only.
0107      */
0108     task1 = find_task_by_vpid(pid1);
0109     task2 = find_task_by_vpid(pid2);
0110     if (!task1 || !task2)
0111         goto err_no_task;
0112 
0113     get_task_struct(task1);
0114     get_task_struct(task2);
0115 
0116     rcu_read_unlock();
0117 
0118     /*
0119      * One should have enough rights to inspect task details.
0120      */
0121     ret = kcmp_lock(&task1->signal->cred_guard_mutex,
0122             &task2->signal->cred_guard_mutex);
0123     if (ret)
0124         goto err;
0125     if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
0126         !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
0127         ret = -EPERM;
0128         goto err_unlock;
0129     }
0130 
0131     switch (type) {
0132     case KCMP_FILE: {
0133         struct file *filp1, *filp2;
0134 
0135         filp1 = get_file_raw_ptr(task1, idx1);
0136         filp2 = get_file_raw_ptr(task2, idx2);
0137 
0138         if (filp1 && filp2)
0139             ret = kcmp_ptr(filp1, filp2, KCMP_FILE);
0140         else
0141             ret = -EBADF;
0142         break;
0143     }
0144     case KCMP_VM:
0145         ret = kcmp_ptr(task1->mm, task2->mm, KCMP_VM);
0146         break;
0147     case KCMP_FILES:
0148         ret = kcmp_ptr(task1->files, task2->files, KCMP_FILES);
0149         break;
0150     case KCMP_FS:
0151         ret = kcmp_ptr(task1->fs, task2->fs, KCMP_FS);
0152         break;
0153     case KCMP_SIGHAND:
0154         ret = kcmp_ptr(task1->sighand, task2->sighand, KCMP_SIGHAND);
0155         break;
0156     case KCMP_IO:
0157         ret = kcmp_ptr(task1->io_context, task2->io_context, KCMP_IO);
0158         break;
0159     case KCMP_SYSVSEM:
0160 #ifdef CONFIG_SYSVIPC
0161         ret = kcmp_ptr(task1->sysvsem.undo_list,
0162                    task2->sysvsem.undo_list,
0163                    KCMP_SYSVSEM);
0164 #else
0165         ret = -EOPNOTSUPP;
0166 #endif
0167         break;
0168     default:
0169         ret = -EINVAL;
0170         break;
0171     }
0172 
0173 err_unlock:
0174     kcmp_unlock(&task1->signal->cred_guard_mutex,
0175             &task2->signal->cred_guard_mutex);
0176 err:
0177     put_task_struct(task1);
0178     put_task_struct(task2);
0179 
0180     return ret;
0181 
0182 err_no_task:
0183     rcu_read_unlock();
0184     return -ESRCH;
0185 }
0186 
0187 static __init int kcmp_cookies_init(void)
0188 {
0189     int i;
0190 
0191     get_random_bytes(cookies, sizeof(cookies));
0192 
0193     for (i = 0; i < KCMP_TYPES; i++)
0194         cookies[i][1] |= (~(~0UL >>  1) | 1);
0195 
0196     return 0;
0197 }
0198 arch_initcall(kcmp_cookies_init);