Skip to contentSkip to conversation

rust: uaccess: avoid unsafe unwrap_unchecked() in strcpy_into_buf()

PATCH In review Thorsten Blum 4 participants 8 messages Oct 8 – Oct 11 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: uaccess: avoid unsafe unwrap_unchecked() in strcpy_into_buf()

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;
         }
 
         // This method consumes `self`, so it can only be called once, thus we do not need to
#1 lore raw
Quoted 13 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
rust/kernel/uaccess.rs +0 -0
index 5f6c4d7a1a51..2a0af795e75d 100644

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 7 lines
rust/kernel/uaccess.rs +1 -3
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

#2 lore raw
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 12 lines
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

I checked the generated code before and after the patch and it is identical since the compiler is able to remove the additional check. Therefore, this removes an unsafe block without adding runtime cost.

It also avoids relying on the buf.is_empty() check to prevent undefined behavior.

reviewer
#3 lore raw
Quoted 27 lines · Thorsten Blum
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 12 lines
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

Quoted 6 lines

I checked the generated code before and after the patch and it is identical since the compiler is able to remove the additional check. Therefore, this removes an unsafe block without adding runtime cost.

It also avoids relying on the buf.is_empty() check to prevent undefined behavior.

Adding an error returning path is worse for something that cannot happen is worse than invoking unsafe in my opinion.

Why not just unwrap?

    *buf.last_mut().unwrap() = 0;

Best,
Gary

#4 lore raw
Quoted 35 lines · Gary Guo
Quoted 27 lines · Thorsten Blum
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 12 lines
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

Quoted 6 lines

I checked the generated code before and after the patch and it is identical since the compiler is able to remove the additional check. Therefore, this removes an unsafe block without adding runtime cost.

It also avoids relying on the buf.is_empty() check to prevent undefined behavior.

Quoted 6 lines

Adding an error returning path is worse for something that cannot happen is worse than invoking unsafe in my opinion.

Why not just unwrap?

    *buf.last_mut().unwrap() = 0;

I guess the author of the code decided to avoid unwrap for the same reason they avoided a runtime error: the first two lines of the method guarantee that the access is valid. Now I wish we could keep the enforcing statement closer to the unsafe block relying it, but I cannot find a better way to write that method. unwrap would just switch the SAFETY statement for a PANIC one.

Honestly I think this code is fine as it is.

reviewer
#5 lore raw
Quoted 43 lines · Alexandre Courbot
Quoted 35 lines · Gary Guo
Quoted 27 lines · Thorsten Blum
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 12 lines
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

Quoted 6 lines

I checked the generated code before and after the patch and it is identical since the compiler is able to remove the additional check. Therefore, this removes an unsafe block without adding runtime cost.

It also avoids relying on the buf.is_empty() check to prevent undefined behavior.

Quoted 6 lines

Adding an error returning path is worse for something that cannot happen is worse than invoking unsafe in my opinion.

Why not just unwrap?

    *buf.last_mut().unwrap() = 0;
Quoted 8 lines

I guess the author of the code decided to avoid unwrap for the same reason they avoided a runtime error: the first two lines of the method guarantee that the access is valid. Now I wish we could keep the enforcing statement closer to the unsafe block relying it, but I cannot find a better way to write that method. unwrap would just switch the SAFETY statement for a PANIC one.

Honestly I think this code is fine as it is.

A NonEmptySlice<T> which is [T] but first/last can just not return Option?

Best,
Gary

#6 lore raw
Quoted 51 lines · Gary Guo
Quoted 50 lines · Alexandre Courbot
Quoted 35 lines · Gary Guo
Quoted 27 lines · Thorsten Blum
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Thorsten Blum

Since strcpy_into_buf() already rejects empty buffers, use ok_or() instead of unwrap_unchecked() when NUL-terminating the buffer.

---
 rust/kernel/uaccess.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

rust/kernel/uaccess.rs pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> { · line 425

Quoted 12 lines
rust/kernel/uaccess.rs +1 -3
index 5f6c4d7a1a51..2a0af795e75d 100644
@@ -422,9 +422,7 @@ pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
             // This means that we filled the buffer exactly. In this case, we add a NUL-terminator
             // and return it. Unlike the `len < dst.len()` branch, don't modify `len` because it
             // already represents the length including the NUL-terminator.
-            //
-            // SAFETY: Due to the check at the beginning, the buffer is not empty.
-            unsafe { *buf.last_mut().unwrap_unchecked() = 0 };
+            *buf.last_mut().ok_or(EINVAL)? = 0;

I am not sure this gives us much - we are trading an unsafe statement that is well-controlled (enforced by the first two lines of the method) for a runtime check. I'd say this is working as intended here.

Quoted 6 lines

I checked the generated code before and after the patch and it is identical since the compiler is able to remove the additional check. Therefore, this removes an unsafe block without adding runtime cost.

It also avoids relying on the buf.is_empty() check to prevent undefined behavior.

Quoted 6 lines

Adding an error returning path is worse for something that cannot happen is worse than invoking unsafe in my opinion.

Why not just unwrap?

    *buf.last_mut().unwrap() = 0;

I guess the author of the code decided to avoid unwrap for the same reason they avoided a runtime error: the first two lines of the method guarantee that the access is valid. Now I wish we could keep the enforcing statement closer to the unsafe block relying it, but I cannot find a better way to write that method. unwrap would just switch the SAFETY statement for a PANIC one.

Quoted 4 lines
Quoted 1 line

Honestly I think this code is fine as it is.

A NonEmptySlice<T> which is [T] but first/last can just not return Option?

I looked it up hoping it already existed. :)

That would definitely be an elegant way to solve this issue (and IMHO should eventually become part of the standard library). This example is actually a great illustration of the panic/unsafe/result dilemma we discussed during Kangrejos (and yes, I'll follow-up with a proposal for guidelines soon, sorry about the delay).

Until we have NonEmptySlice, I'd say the current unsafe or having a panic are morally equivalent, so I don't see a strong justification for this patch.

#7 lore raw
Quoted 14 lines · Alexandre Courbot
Quoted 3 lines · Gary Guo

A NonEmptySlice<T> which is [T] but first/last can just not return Option?

I looked it up hoping it already existed. :)

That would definitely be an elegant way to solve this issue (and IMHO should eventually become part of the standard library). This example is actually a great illustration of the panic/unsafe/result dilemma we discussed during Kangrejos (and yes, I'll follow-up with a proposal for guidelines soon, sorry about the delay).

Quoted 3 lines

Until we have NonEmptySlice, I'd say the current unsafe or having a panic are morally equivalent, so I don't see a strong justification for this patch.

Sounds like something to explore; filled:
https://github.com/Rust-for-Linux/linux/issues/1262

I can convert it to a "good first issue" if you guys prefer.

Cheers,
Miguel