Skip to contentSkip to conversation

cobalt: Improve cobalt_copy_from/to_user

PATCH In review Jan Kiszka 1 participant 2 messages Feb 26, 2019 – Feb 27, 2019

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 cobalt: Improve cobalt_copy_from/to_user

From: Jan Kiszka <[email protected]>

This aligns the definition of our user copy services with the kernel in two aspects:
- use unlikely to mark the fast path
- memset the remaining bits of the target region if the copy fails; this avoids false-positive compiler warnings and reduces the risk to evaluate undefined data in case the return value is not checked

---
 kernel/cobalt/include/asm-generic/xenomai/syscall.h | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
kernel/cobalt/include/asm-generic/xenomai/syscall.h +14 -4
index 7909fc310a..e14a9d1e3b 100644
@@ -46,15 +46,25 @@
 static inline int cobalt_copy_from_user(void *dst, const void __user *src,
 					size_t size)
 {
-	return (!access_rok(src, size) ||
-		__xn_copy_from_user(dst, src, size)) ? -EFAULT : 0;
+	size_t remaining = size;
+
+	if (likely(access_rok(src, size)))
+		remaining = __xn_copy_from_user(dst, src, size);
+
+	if (unlikely(remaining > 0)) {
+		memset(dst + (size - remaining), 0, remaining);
+		return -EFAULT;
+	}
+	return 0;
 }
 
 static inline int cobalt_copy_to_user(void __user *dst, const void *src,
 				      size_t size)
 {
-	return (!access_wok(dst, size) ||
-		__xn_copy_to_user(dst, src, size)) ? -EFAULT : 0;
+	if (unlikely(!access_wok(dst, size) ||
+	    __xn_copy_to_user(dst, src, size)))
+		return -EFAULT;
+	return 0;
 }
 
 static inline int cobalt_strncpy_from_user(char *dst, const char __user *src,
Signature · 1 line
2.16.4
author
#1 lore raw
Quoted 39 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This aligns the definition of our user copy services with the kernel in two aspects:
- use unlikely to mark the fast path
- memset the remaining bits of the target region if the copy fails; this avoids false-positive compiler warnings and reduces the risk to evaluate undefined data in case the return value is not checked

---
  kernel/cobalt/include/asm-generic/xenomai/syscall.h | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
kernel/cobalt/include/asm-generic/xenomai/syscall.h +10 -2
index 7909fc310a..e14a9d1e3b 100644
@@ -46,15 +46,25 @@
  static inline int cobalt_copy_from_user(void *dst, const void __user *src,
  					size_t size)
  {
-	return (!access_rok(src, size) ||
-		__xn_copy_from_user(dst, src, size)) ? -EFAULT : 0;
+	size_t remaining = size;
+
+	if (likely(access_rok(src, size)))
+		remaining = __xn_copy_from_user(dst, src, size);
+
+	if (unlikely(remaining > 0)) {
+		memset(dst + (size - remaining), 0, remaining);
+		return -EFAULT;
+	}
+	return 0;
  }
 
  static inline int cobalt_copy_to_user(void __user *dst, const void *src,
  				      size_t size)

kernel/cobalt/include/asm-generic/xenomai/syscall.h line 70

Quoted 10 lines
kernel/cobalt/include/asm-generic/xenomai/syscall.h +4 -2
  {
-	return (!access_wok(dst, size) ||
-		__xn_copy_to_user(dst, src, size)) ? -EFAULT : 0;
+	if (unlikely(!access_wok(dst, size) ||
+	    __xn_copy_to_user(dst, src, size)))
+		return -EFAULT;
+	return 0;
  }
 
  static inline int cobalt_strncpy_from_user(char *dst, const char __user *src,

FWIW, I've just realized that we still use a lot of rtdm_copy_*_user without rtdm_read/rw_user_ok. Those should be migrated to rtdm_safe_copy_* eventually. I'm even considering to deprecate the split API, but one after the other.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux