[PATCH 15/16] COMPAT: add get/put_compat_rlimit

From: Jiri Slaby
Date: Wed Nov 18 2009 - 09:53:19 EST


Extract those functions from compat_sys_[gs]etrlimit for later
use with newly added rlimit syscalls.

Signed-off-by: Jiri Slaby <jslaby@xxxxxxxxxx>
Cc: James Morris <jmorris@xxxxxxxxx>
Cc: Heiko Carstens <heiko.carstens@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxx>
---
kernel/compat.c | 61 ++++++++++++++++++++++++++++++++++++-------------------
1 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/kernel/compat.c b/kernel/compat.c
index f6c204f..af15719 100644
--- a/kernel/compat.c
+++ b/kernel/compat.c
@@ -274,6 +274,39 @@ asmlinkage long compat_sys_sigprocmask(int how, compat_old_sigset_t __user *set,
return ret;
}

+static int get_compat_rlimit(struct rlimit *dst,
+ const struct compat_rlimit __user *src)
+{
+ if (!access_ok(VERIFY_READ, src, sizeof(*src)) ||
+ __get_user(dst->rlim_cur, &src->rlim_cur) ||
+ __get_user(dst->rlim_max, &src->rlim_max))
+ return -EFAULT;
+
+ if (dst->rlim_cur == COMPAT_RLIM_INFINITY)
+ dst->rlim_cur = RLIM_INFINITY;
+ if (dst->rlim_max == COMPAT_RLIM_INFINITY)
+ dst->rlim_max = RLIM_INFINITY;
+ return 0;
+}
+
+static int put_compat_rlimit(const struct rlimit *src,
+ struct compat_rlimit __user *dst)
+{
+ struct rlimit r = *src;
+
+ if (r.rlim_cur > COMPAT_RLIM_INFINITY)
+ r.rlim_cur = COMPAT_RLIM_INFINITY;
+ if (r.rlim_max > COMPAT_RLIM_INFINITY)
+ r.rlim_max = COMPAT_RLIM_INFINITY;
+
+ if (!access_ok(VERIFY_WRITE, dst, sizeof(*dst)) ||
+ __put_user(r.rlim_cur, &dst->rlim_cur) ||
+ __put_user(r.rlim_max, &dst->rlim_max))
+ return -EFAULT;
+
+ return 0;
+}
+
asmlinkage long compat_sys_setrlimit(unsigned int resource,
struct compat_rlimit __user *rlim)
{
@@ -284,17 +317,12 @@ asmlinkage long compat_sys_setrlimit(unsigned int resource,
if (resource >= RLIM_NLIMITS)
return -EINVAL;

- if (!access_ok(VERIFY_READ, rlim, sizeof(*rlim)) ||
- __get_user(r.rlim_cur, &rlim->rlim_cur) ||
- __get_user(r.rlim_max, &rlim->rlim_max))
- return -EFAULT;
+ ret = get_compat_rlimit(&r, rlim);
+ if (ret)
+ return ret;

- if (r.rlim_cur == COMPAT_RLIM_INFINITY)
- r.rlim_cur = RLIM_INFINITY;
- if (r.rlim_max == COMPAT_RLIM_INFINITY)
- r.rlim_max = RLIM_INFINITY;
set_fs(KERNEL_DS);
- ret = sys_setrlimit(resource, (struct rlimit __user *) &r);
+ ret = sys_setrlimit(resource, (struct rlimit __force __user *)&r);
set_fs(old_fs);
return ret;
}
@@ -336,19 +364,10 @@ asmlinkage long compat_sys_getrlimit (unsigned int resource,
mm_segment_t old_fs = get_fs();

set_fs(KERNEL_DS);
- ret = sys_getrlimit(resource, (struct rlimit __user *) &r);
+ ret = sys_getrlimit(resource, (struct rlimit __force __user *)&r);
set_fs(old_fs);
- if (!ret) {
- if (r.rlim_cur > COMPAT_RLIM_INFINITY)
- r.rlim_cur = COMPAT_RLIM_INFINITY;
- if (r.rlim_max > COMPAT_RLIM_INFINITY)
- r.rlim_max = COMPAT_RLIM_INFINITY;
-
- if (!access_ok(VERIFY_WRITE, rlim, sizeof(*rlim)) ||
- __put_user(r.rlim_cur, &rlim->rlim_cur) ||
- __put_user(r.rlim_max, &rlim->rlim_max))
- return -EFAULT;
- }
+ if (!ret)
+ ret = put_compat_rlimit(&r, rlim);
return ret;
}

--
1.6.4.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/