Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* BUG kernel macro: copy_to_user
@ 1999-04-08  7:52 Andreas Krebs
  1999-04-08 16:44 ` Richard Henderson
  1999-04-14  7:25 ` BUG kernel macro: copy_to_user Richard Henderson
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Krebs @ 1999-04-08  7:52 UTC (permalink / raw)
  To: axp-list

[-- Attachment #1: Type: text/plain, Size: 2175 bytes --]


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.

so i take a look at the copy_to_user macro and notice, that some
registers are used direct. i think the strlen() command somehow messes
up this registers and causes the following __access_ok to fail.
so i changed the macro a bit, by moving the length calc to the top
(the complete patch is attached below):

 #define __copy_tofrom_user(to,from,n,v)                                            \
 ({                                                                         \
-       register long __tmp_len = (n);                                      \
        register void * __cu_to __asm__("$6") = (to);                       \
        register const void * __cu_from __asm__("$7") = (from);             \
-       register long __cu_len __asm__("$0") = __tmp_len;                   \
+       register long __cu_len __asm__("$0") = (n);                         \
        if (__access_ok(((long)(v)),__cu_len,get_fs())) {                   \
 

this solved the problem and the isdnctrl ioctls now seem to work.

maybe someone how knows more about inline assembler should have a look.
i think this can cause a lot of trouble not only with isdn but with other 
drivers also. 

bye,
Andreas


PS:
my machine is a 164SX, the isdn card is a Eicon DIVA PCI. i experienced
the problem with ALL 2.2.* kernels and both egcs 1.0.2 and 1.1.2. 
the patch is against as 2.2.5-ac4 but should also work with other kernels.



[-- Attachment #2: alpha-uaccess.patch.gz --]
[-- Type: application/x-gzip , Size: 520 bytes --]

[-- Attachment #3: Type: text/plain, Size: 266 bytes --]

_____________________________________________________________________________

                               Andreas S. Krebs

                CEC Karlsruhe, SAP AG, krebs@qkal.sap-ag.de
_____________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: BUG kernel macro: copy_to_user
  1999-04-08  7:52 BUG kernel macro: copy_to_user Andreas Krebs
@ 1999-04-08 16:44 ` Richard Henderson
  1999-04-09 17:24   ` Matthew Jacob
  1999-04-14  7:25 ` BUG kernel macro: copy_to_user Richard Henderson
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Henderson @ 1999-04-08 16:44 UTC (permalink / raw)
  To: axp-list

On Thu, Apr 08, 1999 at 09:52:30AM +0200, Andreas Krebs wrote:
> ... so i take a look at the copy_to_user macro and notice, that some
> registers are used direct. i think the strlen() command somehow messes
> up this registers and causes the following __access_ok to fail.
> so i changed the macro a bit, by moving the length calc to the top

Good eyes.  There are about five places in the same file with the
same problem.  I'll fix it all up.

Thanks.


r~


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: BUG kernel macro: copy_to_user
  1999-04-08 16:44 ` Richard Henderson
@ 1999-04-09 17:24   ` Matthew Jacob
  1999-04-09 17:26     ` I *apologize* Matthew Jacob
  0 siblings, 1 reply; 5+ messages in thread
From: Matthew Jacob @ 1999-04-09 17:24 UTC (permalink / raw)
  To: axp-list



> On Thu, Apr 08, 1999 at 09:52:30AM +0200, Andreas Krebs wrote:
> > ... so i take a look at the copy_to_user macro and notice, that some
> > registers are used direct. i think the strlen() command somehow messes
> > up this registers and causes the following __access_ok to fail.
> > so i changed the macro a bit, by moving the length calc to the top
> 
> Good eyes.  There are about five places in the same file with the
> same problem.  I'll fix it all up.
> 

This is an argument for why the CVS stuff is good- you'll make the fix,
and there'll a central place for folks to get stuff within a day rather
than knowing where patches are, when the next microrelease is, etc...
.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* I *apologize*
  1999-04-09 17:24   ` Matthew Jacob
@ 1999-04-09 17:26     ` Matthew Jacob
  0 siblings, 0 replies; 5+ messages in thread
From: Matthew Jacob @ 1999-04-09 17:26 UTC (permalink / raw)
  To: axp-list


This was meant to be a private message to rth- not sent to the list. I
apologize for the noise.


On Fri, 9 Apr 1999, Matthew Jacob wrote:

> 
> 
> > On Thu, Apr 08, 1999 at 09:52:30AM +0200, Andreas Krebs wrote:
> > > ... so i take a look at the copy_to_user macro and notice, that some
> > > registers are used direct. i think the strlen() command somehow messes
> > > up this registers and causes the following __access_ok to fail.
> > > so i changed the macro a bit, by moving the length calc to the top
> > 
> > Good eyes.  There are about five places in the same file with the
> > same problem.  I'll fix it all up.
> > 
> 
> This is an argument for why the CVS stuff is good- you'll make the fix,
> and there'll a central place for folks to get stuff within a day rather
> than knowing where patches are, when the next microrelease is, etc...
> .
> 
> 
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: BUG kernel macro: copy_to_user
  1999-04-08  7:52 BUG kernel macro: copy_to_user Andreas Krebs
  1999-04-08 16:44 ` Richard Henderson
@ 1999-04-14  7:25 ` Richard Henderson
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Henderson @ 1999-04-14  7:25 UTC (permalink / raw)
  To: axp-list; +Cc: krebs, torvalds

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 *);


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1999-04-14  7:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-04-08  7:52 BUG kernel macro: copy_to_user Andreas Krebs
1999-04-08 16:44 ` Richard Henderson
1999-04-09 17:24   ` Matthew Jacob
1999-04-09 17:26     ` I *apologize* Matthew Jacob
1999-04-14  7:25 ` BUG kernel macro: copy_to_user Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox