Skip to contentSkip to conversation

setfsuid02: using -1 as invalid fsuid for setfsuid()

PATCH In review Avinesh Kumar 4 participants 13 messages Oct 27, 2022 – Nov 2, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 setfsuid02: using -1 as invalid fsuid for setfsuid()

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

---
 testcases/kernel/syscalls/setfsuid/setfsuid02.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
testcases/kernel/syscalls/setfsuid/setfsuid02.c +1 -3
index 850f17834..f5aa1c004 100644
@@ -21,9 +21,7 @@ static void run(void)
 	uid_t invalid_uid, current_uid;
 
 	current_uid = geteuid();
-	invalid_uid = 1;
-	while (getpwuid(invalid_uid))
-		invalid_uid++;
+	invalid_uid = -1;
 
 	UID16_CHECK(invalid_uid, setfsuid);
 
Signature · 5 lines
2.38.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Avinesh,

Quoted 5 lines

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

Here was supposed to be

Fixes 85f0b8478 ("setfsuid02: Rewrite using new LTP API")

as the problem was introduced in your rewrite, right?

testcases/kernel/syscalls/setfsuid/setfsuid02.c static void run(void) · line 24

Quoted 12 lines
testcases/kernel/syscalls/setfsuid/setfsuid02.c +1 -3
index 850f17834..f5aa1c004 100644
@@ -21,9 +21,7 @@ static void run(void)
 	uid_t invalid_uid, current_uid;
 
 	current_uid = geteuid();
-	invalid_uid = 1;
-	while (getpwuid(invalid_uid))
-		invalid_uid++;
+	invalid_uid = -1;

IMHO it should be casted

invalid_uid = (uid_t)-1;

as the code in shadow-utils:
https://github.com/shadow-maint/shadow/blob/eaebea55a495a56317ed85e959b3599f73c6bdf2/src/newusers.c#L342-L345

This does not work on 16-bit version, because uid_t is unsigned int, -1 overflows the allowed value when we do check:

UID16_CHECK(invalid_uid, setfsuid);

setfsuid02.c:26: TBROK: uid -1 of invalid_uid is too large for testing 16-bit version of setfsuid()

It also does not make sense to check invalid_uid, it should have been current_uid in 85f0b8478 (my bad not catching this):

UID16_CHECK(current_uid, setfsuid);

Please, always try to test 16-bit variant (most of us does not bother to test 32-bit compatibility, but at least to see TCONF is worth).

If you agree, I merge it with changes below.

Kind regards,
Petr

kernel/syscalls/setfsuid/setfsuid02.c +2 -2
index f5aa1c004..92e1979fa 100644
@@ -21,9 +21,9 @@ static void run(void)
 	uid_t invalid_uid, current_uid;
 
 	current_uid = geteuid();
-	invalid_uid = -1;
+	invalid_uid = (uid_t)-1;
 
-	UID16_CHECK(invalid_uid, setfsuid);
+	UID16_CHECK(current_uid, setfsuid);
 
 	TST_EXP_VAL_SILENT(SETFSUID(invalid_uid), current_uid);
 	TST_EXP_VAL(SETFSUID(invalid_uid), current_uid,
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 8 lines · Petr Vorel

Hi Avinesh,

Quoted 5 lines

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

Quoted 4 lines

Here was supposed to be

Fixes 85f0b8478 ("setfsuid02: Rewrite using new LTP API")

as the problem was introduced in your rewrite, right?

No, the original test was already broken, it just didn't do any real failure checks so it always passed.

Quoted 4 lines

It also does not make sense to check invalid_uid, it should have been current_uid in 85f0b8478 (my bad not catching this):

UID16_CHECK(current_uid, setfsuid);

No, UID16_CHECK(invalid_uid, setfsuid); is the correct test call. The test is supposed to verify that trying to set invalid_uid will fail, and the only way to verify that it failed is to call setfsuid(invalid_uid) again and check that it returns current_uid.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Martin, Avinesh,

Quoted 9 lines · Petr Vorel
Quoted 7 lines

Hi Avinesh,

Quoted 5 lines

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

Quoted 7 lines
Quoted 4 lines

Here was supposed to be

Fixes 85f0b8478 ("setfsuid02: Rewrite using new LTP API")

as the problem was introduced in your rewrite, right?

No, the original test was already broken, it just didn't do any real failure checks so it always passed.

Right, thanks!

Quoted 9 lines
Quoted 4 lines

It also does not make sense to check invalid_uid, it should have been current_uid in 85f0b8478 (my bad not catching this):

UID16_CHECK(current_uid, setfsuid);

No, UID16_CHECK(invalid_uid, setfsuid); is the correct test call. The test is supposed to verify that trying to set invalid_uid will fail, and the only way to verify that it failed is to call setfsuid(invalid_uid) again and check that it returns current_uid.

I'm somehow blind today. UID16_CHECK() in compat_tst_16.h calls UID_SIZE_CHECK(), which just checks the uid value. I don't see setfsuid() call, IMHO that's done in SETFSUID().

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 7 lines · Petr Vorel
Quoted 4 lines

No, UID16_CHECK(invalid_uid, setfsuid); is the correct test call. The test is supposed to verify that trying to set invalid_uid will fail, and the only way to verify that it failed is to call setfsuid(invalid_uid) again and check that it returns current_uid.

I'm somehow blind today. UID16_CHECK() in compat_tst_16.h calls UID_SIZE_CHECK(), which just checks the uid value. I don't see setfsuid() call, IMHO that's done in SETFSUID().

It doesn't make sense to call UID16_CHECK() on a value that we'll never pass to a 16bit syscall, does it?

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 11 lines · Petr Vorel
Quoted 7 lines
Quoted 4 lines

No, UID16_CHECK(invalid_uid, setfsuid); is the correct test call. The test is supposed to verify that trying to set invalid_uid will fail, and the only way to verify that it failed is to call setfsuid(invalid_uid) again and check that it returns current_uid.

I'm somehow blind today. UID16_CHECK() in compat_tst_16.h calls UID_SIZE_CHECK(), which just checks the uid value. I don't see setfsuid() call, IMHO that's done in SETFSUID().

It doesn't make sense to call UID16_CHECK() on a value that we'll never pass to a 16bit syscall, does it?

Ah, sure, I see current_uid is used for checking only. So what is your suggestion to fix the problem on 16-bit?

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
Quoted 2 lines · Petr Vorel

Ah, sure, I see current_uid is used for checking only. So what is your suggestion to fix the problem on 16-bit?

I guess using UID_T (defined in the LTP compat headers) instead of uid_t is the solution, then -1 will be cast to the correct bitsize.

Signature · 11 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
Quoted 6 lines · Petr Vorel
Quoted 2 lines

Ah, sure, I see current_uid is used for checking only. So what is your suggestion to fix the problem on 16-bit?

I guess using UID_T (defined in the LTP compat headers) instead of uid_t is the solution, then -1 will be cast to the correct bitsize.

Thank you, that worked.

Merged, with additional cast to long to keep compiler happy.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Hi Petr, Martin,

Thank you for the discussion and fixing the patch for 16-bit. I am making a note to always test 16-bit binaries also.

Quoted 7 lines · IST Petr Vorel
Quoted 6 lines · Petr Vorel
Quoted 2 lines

Ah, sure, I see current_uid is used for checking only. So what is your suggestion to fix the problem on 16-bit?

I guess using UID_T (defined in the LTP compat headers) instead of uid_t is the solution, then -1 will be cast to the correct bitsize.

Quoted 7 lines

Thank you, that worked.

Merged, with additional cast to long to keep compiler happy.

Kind regards,
Petr

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw
In reply to Avinesh Kumar

Hello,

Quoted 5 lines · Avinesh Kumar

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

I think the original test is flawed and testing what using -1 does is not very interesting as the kernel uses standard boilerplate to deal with this.

AFAICT we don't test what happens if a non-root user tries to set the fsuid to a uid that is not the euid, ruid or saved uid or 0/-1.

Possibly that is something for a new test though.

Quoted 23 lines
---
 testcases/kernel/syscalls/setfsuid/setfsuid02.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
testcases/kernel/syscalls/setfsuid/setfsuid02.c +1 -3
index 850f17834..f5aa1c004 100644
@@ -21,9 +21,7 @@ static void run(void)
 	uid_t invalid_uid, current_uid;
 
 	current_uid = geteuid();
-	invalid_uid = 1;
-	while (getpwuid(invalid_uid))
-		invalid_uid++;
+	invalid_uid = -1;
 
 	UID16_CHECK(invalid_uid, setfsuid);
 
Signature · 1 line
2.38.0
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw
Quoted 10 lines

Hello,

Quoted 5 lines · Avinesh Kumar

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

Quoted 8 lines

I think the original test is flawed and testing what using -1 does is not very interesting as the kernel uses standard boilerplate to deal with this.

AFAICT we don't test what happens if a non-root user tries to set the fsuid to a uid that is not the euid, ruid or saved uid or 0/-1.

Possibly that is something for a new test though.

Ah, sorry, I overlooked this, merged now.

Kind regards,
Petr

Quoted 22 lines
Quoted 22 lines
---
 testcases/kernel/syscalls/setfsuid/setfsuid02.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
testcases/kernel/syscalls/setfsuid/setfsuid02.c +1 -3
index 850f17834..f5aa1c004 100644
@@ -21,9 +21,7 @@ static void run(void)
 	uid_t invalid_uid, current_uid;
 
 	current_uid = geteuid();
-	invalid_uid = 1;
-	while (getpwuid(invalid_uid))
-		invalid_uid++;
+	invalid_uid = -1;
 
 	UID16_CHECK(invalid_uid, setfsuid);
 
Signature · 1 line
2.38.0
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Hello,

Quoted 14 lines · Petr Vorel
Quoted 13 lines

Hello,

Quoted 5 lines · Avinesh Kumar

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

I think the original test is flawed and testing what using -1 does is not very interesting as the kernel uses standard boilerplate to deal with this.

Quoted 6 lines
Quoted 4 lines

AFAICT we don't test what happens if a non-root user tries to set the fsuid to a uid that is not the euid, ruid or saved uid or 0/-1.

Possibly that is something for a new test though.

Ah, sorry, I overlooked this, merged now.

No problem, I think that is the correct thing to do. I'll leave it to Avinesh or someone else to extend the test (or not).

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

Hi Richie,

Quoted 23 lines · IST Richard Palethorpe

Hello,

Quoted 19 lines · Petr Vorel
Quoted 19 lines

Hello,

Quoted 5 lines · Avinesh Kumar

a uid which does not have an entry in the /etc/passwd file is not really an invalid fsuid for setfsuid(), so changing the test to use -1 as an invalid fsuid.
And second setfsuid(-1) call is to verify that preceding call has actually failed and there is no change in the fsuid.

I think the original test is flawed and testing what using -1 does is not very interesting as the kernel uses standard boilerplate to deal with this.

AFAICT we don't test what happens if a non-root user tries to set the fsuid to a uid that is not the euid, ruid or saved uid or 0/-1.

Possibly that is something for a new test though.

I'll post a new patch for this. Thank you for the suggestion.

Quoted 7 lines
Quoted 2 lines

Ah, sorry, I overlooked this, merged now.

No problem, I think that is the correct thing to do. I'll leave it to Avinesh or someone else to extend the test (or not).

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp