Skip to contentSkip to conversation

drivers/tty/vt: copy userspace arrays safely

PATCH In review Philipp Stanner 3 participants 5 messages Nov 2, 2023 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/tty/vt: copy userspace arrays safely

The functions (v)memdup_user() are utilized to copy userspace arrays. This is done without overflow checks.

Use the new wrappers memdup_array_user() and vmemdup_array_user() to copy the arrays more safely.

---
 drivers/tty/vt/consolemap.c | 2 +-
 drivers/tty/vt/keyboard.c   | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/tty/vt/consolemap.c +1 -1
index f02d21e2a96e..313cef3322eb 100644
@@ -644,7 +644,7 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
 	if (!ct)
 		return 0;
 
-	unilist = vmemdup_user(list, array_size(sizeof(*unilist), ct));
+	unilist = vmemdup_array_user(list, ct, sizeof(*unilist));
 	if (IS_ERR(unilist))
 		return PTR_ERR(unilist);
 
drivers/tty/vt/keyboard.c +4 -4
index 1fe6107b539b..802ceb0a5e4c 100644
@@ -1773,8 +1773,8 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 
 		if (ct) {
 
-			dia = memdup_user(a->kbdiacr,
-					sizeof(struct kbdiacr) * ct);
+			dia = memdup_array_user(a->kbdiacr,
+						ct, sizeof(struct kbdiacr));
 			if (IS_ERR(dia))
 				return PTR_ERR(dia);
 
@@ -1811,8 +1811,8 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;
 
 		if (ct) {
-			buf = memdup_user(a->kbdiacruc,
-					  ct * sizeof(struct kbdiacruc));
+			buf = memdup_array_user(a->kbdiacruc,
+						ct, sizeof(struct kbdiacruc));
 			if (IS_ERR(buf))
 				return PTR_ERR(buf);
 		} 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 7 lines · Philipp Stanner

The functions (v)memdup_user() are utilized to copy userspace arrays. This is done without overflow checks.

Use the new wrappers memdup_array_user() and vmemdup_array_user() to copy the arrays more safely.

drivers/tty/vt/consolemap.c int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list) · line 649

Quoted 7 lines
drivers/tty/vt/consolemap.c +1 -1
@@ -644,7 +644,7 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
 	if (!ct)
 		return 0;
 
-	unilist = vmemdup_user(list, array_size(sizeof(*unilist), ct));
+	unilist = vmemdup_array_user(list, ct, sizeof(*unilist));
 	if (IS_ERR(unilist))
 		return PTR_ERR(unilist);

a 16bit value times sizeof(something).

drivers/tty/vt/keyboard.c int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)

Quoted 5 lines
drivers/tty/vt/keyboard.c +0 -0
index 1fe6107b539b..802ceb0a5e4c 100644
@@ -1773,8 +1773,8 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)

... and here we have
if (ct >= MAX_DIACR)
return -EINVAL;

directly upstream, so it's even better - a value below 256 times sizeof(something)

drivers/tty/vt/keyboard.c int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm) · line 1811

Quoted 11 lines
drivers/tty/vt/keyboard.c +2 -2
 		if (ct) {
 
-			dia = memdup_user(a->kbdiacr,
-					sizeof(struct kbdiacr) * ct);
+			dia = memdup_array_user(a->kbdiacr,
+						ct, sizeof(struct kbdiacr));
 			if (IS_ERR(dia))
 				return PTR_ERR(dia);
 
@@ -1811,8 +1811,8 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;

Ditto.

Quoted 5 lines
drivers/tty/vt/keyboard.c +2 -2
 		if (ct) {
-			buf = memdup_user(a->kbdiacruc,
-					  ct * sizeof(struct kbdiacruc));
+			buf = memdup_array_user(a->kbdiacruc,
+						ct, sizeof(struct kbdiacruc));
#2 lore raw
Quoted 12 lines · Al Viro
Quoted 9 lines · Philipp Stanner

The functions (v)memdup_user() are utilized to copy userspace arrays. This is done without overflow checks.

Use the new wrappers memdup_array_user() and vmemdup_array_user() to copy the arrays more safely.

drivers/tty/vt/consolemap.c +0 -0
@@ -644,7 +644,7 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
      if (!ct)
              return 0;
Quoted 6 lines

drivers/tty/vt/consolemap.c int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list) · line 649

Quoted 4 lines
drivers/tty/vt/consolemap.c +1 -1
-     unilist = vmemdup_user(list, array_size(sizeof(*unilist), ct));
+     unilist = vmemdup_array_user(list, ct, sizeof(*unilist));
      if (IS_ERR(unilist))
              return PTR_ERR(unilist);

a 16bit value times sizeof(something).

So since it's already using array_size here, moving it to a new helper for consistency just makes things clearer, and so you are fine with the patch?

Otherwise I'd think you are been a snarky asshole to a coworker.

Dave.

#3 lore raw
Quoted 18 lines · David Airlie
Quoted 17 lines · Al Viro
Quoted 14 lines · Philipp Stanner

The functions (v)memdup_user() are utilized to copy userspace arrays. This is done without overflow checks.

Use the new wrappers memdup_array_user() and vmemdup_array_user() to copy the arrays more safely.

drivers/tty/vt/consolemap.c +1 -1
@@ -644,7 +644,7 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
      if (!ct)
              return 0;
 
-     unilist = vmemdup_user(list, array_size(sizeof(*unilist), ct));
+     unilist = vmemdup_array_user(list, ct, sizeof(*unilist));
      if (IS_ERR(unilist))
              return PTR_ERR(unilist);
Quoted 5 lines
Quoted 1 line

a 16bit value times sizeof(something).

So since it's already using array_size here, moving it to a new helper for consistency just makes things clearer, and so you are fine with the patch?

Sigh... OK, if you want it spelled out, there we go. I have no objections to the contents of patches; e.g. in case of ppp ioctl it saves the reader a grep in search of structure definitions, which is a good thing. The one and only suggestion I have for those patches is that such patches might be better off with explicit "in this case the overflow is avoided due to <reasons>, but use of this helper makes it obviously safe" - or, in case of real bugs, "the overflow is, indeed, possible here", in which case
Fixes: ... and Cc: stable might be in order.

#4 lore raw
Quoted 31 lines · Al Viro
Quoted 23 lines · David Airlie
Quoted 21 lines · Al Viro
Quoted 18 lines · Philipp Stanner

The functions (v)memdup_user() are utilized to copy userspace arrays.
This is done without overflow checks.

Use the new wrappers memdup_array_user() and
vmemdup_array_user() to
copy the arrays more safely.

drivers/tty/vt/consolemap.c +1 -1
@@ -644,7 +644,7 @@ int con_set_unimap(struct vc_data *vc,
 ushort ct, struct unipair __user *list)
       if (!ct)
               return 0;
 
-     unilist = vmemdup_user(list, array_size(sizeof(*unilist),
 ct));
+     unilist = vmemdup_array_user(list, ct, sizeof(*unilist));
       if (IS_ERR(unilist))

              return PTR_ERR(unilist);

Quoted 6 lines
Quoted 1 line

a 16bit value times sizeof(something).

So since it's already using array_size here, moving it to a new helper
for consistency just makes things clearer, and so you are fine with the patch?

Quoted 14 lines

Sigh...  OK, if you want it spelled out, there we go.  I have no objections
to the contents of patches; e.g. in case of ppp ioctl it saves the reader
a grep in search of structure definitions, which is a good thing. The one
and only suggestion I have for those patches is that such patches might be
better off with explicit "in this case the overflow is avoided due to <reasons>, but use of this helper makes it obviously safe" - or, in case
of real bugs, "the overflow is, indeed, possible here", in which case
Fixes: ... and Cc: stable might be in order.

So if you agree the content is improving things a little bit then it seems the only critical thing is the commit message :)

So let's get that fixed, shifting the focus from security to readability and general usefulness.

Do you have a proposal for a good wording?

Personally, I would have gone with something minimalistic like here in my other commit, where the irrelevance of the overflow-aspect was more obvious for me to see [1]
I can also add a sentence clarifying that it's about improving readability or sth if you think that's better

Kind regards,
P.

[1] https://lore.kernel.org/all/[email protected]/