Skip to contentSkip to conversation

Intel MIC host driver: possible signed underflow (undefined behavior) in userspace API

DISCUSSION Mathieu Desnoyers 3 participants 4 messages Jan 10, 2014 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Hi,

Looking at this commit:

commit f69bcbf3b4c4b333dcd7a48eaf868bf0c88edab5

Author: Ashutosh Dixit <[email protected]>
Date:   Thu Sep 5 16:42:18 2013 -0700

Intel MIC Host Driver Changes for Virtio Devices.

Especially at:

+struct mic_copy_desc {
+#ifdef __KERNEL__
+ struct iovec __user *iov;
+#else
+ struct iovec *iov;
+#endif

+       int iovcnt;
+       __u8 vr_idx;
+       __u8 update_used;
+       __u32 out_len;

+};

Seeing iovcnt being declared as a signed integer seems strange. The first question would be: why is it signed rather than unsigned ?

Then, looking further into

drivers/misc/mic/host/mic_virtio.c:_mic_virtio_copy()

We can see that the while() loop iterates until the local variable iovcnt reaches the value 0 (and iovcnt is also a signed integer). If user-space passes e.g. INT_MIN as iovcnt field, this loop then appears to depend on an undefined behavior (signed underflow) to complete. Wouldn't it be better to use an unsigned integers both in the userspace API and for the local variable ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#2 lore raw
Quoted 29 lines · Mathieu Desnoyers

Hi,

Looking at this commit:

commit f69bcbf3b4c4b333dcd7a48eaf868bf0c88edab5

Author: Ashutosh Dixit <[email protected]>
Date:   Thu Sep 5 16:42:18 2013 -0700

Intel MIC Host Driver Changes for Virtio Devices.

Especially at:

(quoted diff) +11 -0
+struct mic_copy_desc {
+#ifdef __KERNEL__
+       struct iovec __user *iov;
+#else
+       struct iovec *iov;
+#endif
+       int iovcnt;
+       __u8 vr_idx;
+       __u8 update_used;
+       __u32 out_len;
+};

Seeing iovcnt being declared as a signed integer seems strange. The first question would be: why is it signed rather than unsigned ?

Then, looking further into

Quoted 8 lines

drivers/misc/mic/host/mic_virtio.c:_mic_virtio_copy()

We can see that the while() loop iterates until the local variable iovcnt reaches the value 0 (and iovcnt is also a signed integer). If user-space passes e.g. INT_MIN as iovcnt field, this loop then appears to depend on an undefined behavior (signed underflow) to complete. Wouldn't it be better to use an unsigned integers both in the userspace API and for the local variable ?

Better yet, it should be a "__" type variable, as "int" doesn't mean much when crossing the user/kernel boundry...

thanks,

greg k-h

#3 lore raw
Quoted 32 lines · Greg Kroah-Hartman
Quoted 31 lines · Mathieu Desnoyers

Hi,

Looking at this commit:

commit f69bcbf3b4c4b333dcd7a48eaf868bf0c88edab5

Author: Ashutosh Dixit <[email protected]>
Date:   Thu Sep 5 16:42:18 2013 -0700

Intel MIC Host Driver Changes for Virtio Devices.

Especially at:

(quoted diff) +11 -0
+struct mic_copy_desc {
+#ifdef __KERNEL__
+       struct iovec __user *iov;
+#else
+       struct iovec *iov;
+#endif
+       int iovcnt;
+       __u8 vr_idx;
+       __u8 update_used;
+       __u32 out_len;
+};

Seeing iovcnt being declared as a signed integer seems strange. The first question would be: why is it signed rather than unsigned ?

Then, looking further into

drivers/misc/mic/host/mic_virtio.c:_mic_virtio_copy()

Quoted 10 lines
Quoted 6 lines

We can see that the while() loop iterates until the local variable iovcnt reaches the value 0 (and iovcnt is also a signed integer). If user-space passes e.g. INT_MIN as iovcnt field, this loop then appears to depend on an undefined behavior (signed underflow) to complete. Wouldn't it be better to use an unsigned integers both in the userspace API and for the local variable ?

Better yet, it should be a "__" type variable, as "int" doesn't mean much when crossing the user/kernel boundry...

We had designed the interface to be similar to readv(..)/writev(..) which takes an integer iovcnt. However, the suggestion to use __u32 works nicely since it avoids adding the missing integer parameter validation in the driver. We will post a patch incorporating this feedback for the next kernel release.

Thanks to Mathieu for reporting this issue.

Sudeep Dutt

#4 lore raw
Quoted 14 lines · Sudeep Dutt
Quoted 13 lines · Greg Kroah-Hartman
Quoted 12 lines · Mathieu Desnoyers

Hi,

Looking at this commit:

commit f69bcbf3b4c4b333dcd7a48eaf868bf0c88edab5

Author: Ashutosh Dixit <[email protected]>
Date:   Thu Sep 5 16:42:18 2013 -0700

Intel MIC Host Driver Changes for Virtio Devices.

Especially at:

Quoted 11 lines
Quoted 11 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+struct mic_copy_desc {
+#ifdef __KERNEL__
+       struct iovec __user *iov;
+#else
+       struct iovec *iov;
+#endif
+       int iovcnt;
+       __u8 vr_idx;
+       __u8 update_used;
+       __u32 out_len;
+};

Oh, there's also the obvious 32/64 bit userspace/kernel issue here as well, but I'm sure you all know how to handle that properly. It would be nice to fix that up, if you can change the ABI still.

thanks,

greg k-h