From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 10959 invoked from network); 14 Apr 1999 07:48:47 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 14 Apr 1999 07:48:47 -0000 Received: from piglet.twiddle.net (rth@piglet.twiddle.net [207.104.6.26]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id DAA25402 for ; Wed, 14 Apr 1999 03:26:02 -0400 Received: (from rth@localhost) by piglet.twiddle.net (8.8.7/8.8.7) id AAA13211; Wed, 14 Apr 1999 00:25:57 -0700 Message-ID: <19990414002557.A13199@twiddle.net> Date: Wed, 14 Apr 1999 00:25:57 -0700 From: Richard Henderson To: axp-list@redhat.com Cc: krebs@qkal.sap-ag.de, torvalds@transmeta.com Subject: Re: BUG kernel macro: copy_to_user References: <199904080752.JAA0000007399@storm.qkal.sap-ag.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.93.2 In-Reply-To: <199904080752.JAA0000007399@storm.qkal.sap-ag.de>; from Andreas Krebs on Thu, Apr 08, 1999 at 09:52:30AM +0200 On Thu, Apr 08, 1999 at 09:52:30AM +0200, Andreas Krebs wrote: > i think i found a serious bug in the __copy_tofrom_user macro define > in include/asm-alpha/uaccess.h > > i was trying to install isdn under alpha linux and noticed that some > ioctls from isdnctrl failed without any reason, so i looked into the > isdn_common.c code and notice that calls to copy_to_user failed. > > all the calls that failed had in common that the size was NOT constant, > in this case the call was as follows: > if (copy_to_user((char *) arg, s, strlen(s) + 1)){ > ret = -EFAULT; > } else { > ret = 0; > } > > i checked all addresses, ranges and the statement failed all the time > until i changed strlen(s)+1 into a constant. Here's a proper fix for all four instances of this problem. Rather than play unneeded games with macro evaluation, I've just turned all the offenders into inline functions. r~ diff -rup 2.2.5/include/asm-alpha/uaccess.h 2.2.5-axp/include/asm-alpha/uaccess.h --- 2.2.5/include/asm-alpha/uaccess.h Sun Apr 11 17:48:36 1999 +++ 2.2.5-axp/include/asm-alpha/uaccess.h Tue Apr 13 08:55:50 1999 @@ -358,43 +358,55 @@ __asm__ __volatile__("1: stb %r2,%1\n" * Complex access routines */ -#define __copy_to_user(to,from,n) __copy_tofrom_user_nocheck((to),(from),(n)) -#define __copy_from_user(to,from,n) __copy_tofrom_user_nocheck((to),(from),(n)) - -#define copy_to_user(to,from,n) __copy_tofrom_user((to),(from),(n),__cu_to) -#define copy_from_user(to,from,n) __copy_tofrom_user((to),(from),(n),__cu_from) - extern void __copy_user(void); -#define __copy_tofrom_user_nocheck(to,from,n) \ -({ \ - register void * __cu_to __asm__("$6") = (to); \ - register const void * __cu_from __asm__("$7") = (from); \ - register long __cu_len __asm__("$0") = (n); \ - __asm__ __volatile__( \ - "jsr $28,(%3),__copy_user" \ - : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to) \ - : "r" (__copy_user), "0" (__cu_len), \ - "1" (__cu_from), "2" (__cu_to) \ - : "$1","$2","$3","$4","$5","$28","memory"); \ - __cu_len; \ -}) +extern inline long +__copy_tofrom_user_nocheck(void *to, const void *from, long len) +{ + register void * __cu_to __asm__("$6") = to; + register const void * __cu_from __asm__("$7") = from; + register long __cu_len __asm__("$0") = len; + + __asm__ __volatile__( + "jsr $28,__copy_user" + : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to) + : "0" (__cu_len), "1" (__cu_from), "2" (__cu_to) + : "$1","$2","$3","$4","$5","$27","$28","memory"); + + return __cu_len; +} + +extern inline long +__copy_tofrom_user(void *to, const void *from, long len, const void *validate) +{ + if (__access_ok((long)validate, len, get_fs())) { + register void * __cu_to __asm__("$6") = to; + register const void * __cu_from __asm__("$7") = from; + register long __cu_len __asm__("$0") = len; + __asm__ __volatile__( + "jsr $28,__copy_user" + : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to) + : "0" (__cu_len), "1" (__cu_from), "2" (__cu_to) + : "$1","$2","$3","$4","$5","$27","$28","memory"); + len = __cu_len; + } + return len; +} -#define __copy_tofrom_user(to,from,n,v) \ -({ \ - register void * __cu_to __asm__("$6") = (to); \ - register const void * __cu_from __asm__("$7") = (from); \ - register long __cu_len __asm__("$0") = (n); \ - if (__access_ok(((long)(v)),__cu_len,get_fs())) { \ - __asm__ __volatile__( \ - "jsr $28,(%3),__copy_user" \ - : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to) \ - : "r" (__copy_user), "0" (__cu_len), \ - "1" (__cu_from), "2" (__cu_to) \ - : "$1","$2","$3","$4","$5","$28","memory"); \ - } \ - __cu_len; \ -}) +#define __copy_to_user(to,from,n) __copy_tofrom_user_nocheck((to),(from),(n)) +#define __copy_from_user(to,from,n) __copy_tofrom_user_nocheck((to),(from),(n)) + +extern inline long +copy_to_user(void *to, const void *from, long n) +{ + return __copy_tofrom_user(to, from, n, to); +} + +extern inline long +copy_from_user(void *to, const void *from, long n) +{ + return __copy_tofrom_user(to, from, n, from); +} #define copy_to_user_ret(to,from,n,retval) ({ \ if (copy_to_user(to,from,n)) \ @@ -408,46 +420,48 @@ if (copy_from_user(to,from,n)) \ extern void __do_clear_user(void); -#define __clear_user(to,n) \ -({ \ - register void * __cl_to __asm__("$6") = (to); \ - register long __cl_len __asm__("$0") = (n); \ - __asm__ __volatile__( \ - "jsr $28,(%2),__do_clear_user" \ - : "=r"(__cl_len), "=r"(__cl_to) \ - : "r"(__do_clear_user), "0"(__cl_len), "1"(__cl_to) \ - : "$1","$2","$3","$4","$5","$28","memory"); \ - __cl_len; \ -}) - -#define clear_user(to,n) \ -({ \ - register void * __cl_to __asm__("$6") = (to); \ - register long __cl_len __asm__("$0") = (n); \ - if (__access_ok(((long)__cl_to),__cl_len,get_fs())) { \ - __asm__ __volatile__( \ - "jsr $28,(%2),__do_clear_user" \ - : "=r"(__cl_len), "=r"(__cl_to) \ - : "r"(__do_clear_user), "0"(__cl_len), "1"(__cl_to)\ - : "$1","$2","$3","$4","$5","$28","memory"); \ - } \ - __cl_len; \ -}) +extern inline long +__clear_user(void *to, long len) +{ + register void * __cl_to __asm__("$6") = to; + register long __cl_len __asm__("$0") = len; + __asm__ __volatile__( + "jsr $28,__do_clear_user" + : "=r"(__cl_len), "=r"(__cl_to) + : "0"(__cl_len), "1"(__cl_to) + : "$1","$2","$3","$4","$5","$27","$28","memory"); + return __cl_len; +} + +extern inline long +clear_user(void *to, long len) +{ + if (__access_ok((long)to, len, get_fs())) { + register void * __cl_to __asm__("$6") = to; + register long __cl_len __asm__("$0") = len; + __asm__ __volatile__( + "jsr $28,__do_clear_user" + : "=r"(__cl_len), "=r"(__cl_to) + : "0"(__cl_len), "1"(__cl_to) + : "$1","$2","$3","$4","$5","$27","$28","memory"); + len = __cl_len; + } + return len; +} /* Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else strlen. */ extern long __strncpy_from_user(char *__to, const char *__from, long __to_len); -#define strncpy_from_user(to,from,n) \ -({ \ - char * __sfu_to = (to); \ - const char * __sfu_from = (from); \ - long __sfu_ret = -EFAULT; \ - if (__access_ok(((long)__sfu_from),0,get_fs())) \ - __sfu_ret = __strncpy_from_user(__sfu_to,__sfu_from,(n)); \ - __sfu_ret; \ -}) +extern inline long +strncpy_from_user(char *to, const char *from, long n) +{ + long ret = -EFAULT; + if (__access_ok((long)from, 0, get_fs())) + ret = __strncpy_from_user(to, from, n); + return ret; +} /* Returns: 0 if bad, string length+1 (memory size) of string if ok */ extern long __strlen_user(const char *);