public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API
@ 2026-03-10  3:14 Yi Lai
  2026-03-31 20:10 ` Shuah Khan
  0 siblings, 1 reply; 3+ messages in thread
From: Yi Lai @ 2026-03-10  3:14 UTC (permalink / raw)
  To: brauner, shuah, areber, linux-kernel, linux-kselftest, yi1.lai,
	yi1.lai

The clone3_cap_checkpoint_restore.c selftest defined its own 'struct
libcap' to manually cast and manipulate the internal memory layout of
the opaque 'cap_t' type.

Starting with libcap v2.60 (commit aca076443591 "Make cap_t operations
thread safe"), a '__u8 mutex' was added to the beginning of the internal
'struct _cap_struct'. This structural shift breaks the selftest's custom
struct layout assumptions, causing capability corruption and leading to
failures during test execution.

Fix this by removing the custom struct definition and use the standard
cap_set_flag() API.

Fixes: 1d27a0be16d6 ("selftests: add clone3() CAP_CHECKPOINT_RESTORE test")
Signed-off-by: Yi Lai <yi1.lai@intel.com>
---
 .../clone3/clone3_cap_checkpoint_restore.c    | 20 ++++++-------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c
index e82281efa273..e9dbb95181ee 100644
--- a/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c
+++ b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c
@@ -87,15 +87,13 @@ static int test_clone3_set_tid(struct __test_metadata *_metadata,
 	return ret;
 }
 
-struct libcap {
-	struct __user_cap_header_struct hdr;
-	struct __user_cap_data_struct data[2];
-};
+#ifndef CAP_CHECKPOINT_RESTORE
+#define CAP_CHECKPOINT_RESTORE 40
+#endif
 
 static int set_capability(void)
 {
-	cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID };
-	struct libcap *cap;
+	cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID, CAP_CHECKPOINT_RESTORE };
 	int ret = -1;
 	cap_t caps;
 
@@ -111,14 +109,8 @@ static int set_capability(void)
 		goto out;
 	}
 
-	cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
-	cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET);
-
-	cap = (struct libcap *) caps;
-
-	/* 40 -> CAP_CHECKPOINT_RESTORE */
-	cap->data[1].effective |= 1 << (40 - 32);
-	cap->data[1].permitted |= 1 << (40 - 32);
+	cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_values, CAP_SET);
+	cap_set_flag(caps, CAP_PERMITTED, 3, cap_values, CAP_SET);
 
 	if (cap_set_proc(caps)) {
 		perror("cap_set_proc");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API
  2026-03-10  3:14 [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API Yi Lai
@ 2026-03-31 20:10 ` Shuah Khan
  2026-04-01  1:11   ` Lai, Yi
  0 siblings, 1 reply; 3+ messages in thread
From: Shuah Khan @ 2026-03-31 20:10 UTC (permalink / raw)
  To: Yi Lai, brauner, shuah, areber, linux-kernel, linux-kselftest,
	yi1.lai
  Cc: Shuah Khan

On 3/9/26 21:14, Yi Lai wrote:
> The clone3_cap_checkpoint_restore.c selftest defined its own 'struct
> libcap' to manually cast and manipulate the internal memory layout of
> the opaque 'cap_t' type.
> 
> Starting with libcap v2.60 (commit aca076443591 "Make cap_t operations
> thread safe"), a '__u8 mutex' was added to the beginning of the internal
> 'struct _cap_struct'. This structural shift breaks the selftest's custom
> struct layout assumptions, causing capability corruption and leading to
> failures during test execution.
> 
> Fix this by removing the custom struct definition and use the standard
> cap_set_flag() API.

What king of failures are you seeing? Can you send the failures from
the run?

> 
> Fixes: 1d27a0be16d6 ("selftests: add clone3() CAP_CHECKPOINT_RESTORE test")
> Signed-off-by: Yi Lai <yi1.lai@intel.com>
> ---

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API
  2026-03-31 20:10 ` Shuah Khan
@ 2026-04-01  1:11   ` Lai, Yi
  0 siblings, 0 replies; 3+ messages in thread
From: Lai, Yi @ 2026-04-01  1:11 UTC (permalink / raw)
  To: Shuah Khan; +Cc: brauner, shuah, areber, linux-kernel, linux-kselftest, yi1.lai

On Tue, Mar 31, 2026 at 02:10:09PM -0600, Shuah Khan wrote:
> On 3/9/26 21:14, Yi Lai wrote:
> > The clone3_cap_checkpoint_restore.c selftest defined its own 'struct
> > libcap' to manually cast and manipulate the internal memory layout of
> > the opaque 'cap_t' type.
> > 
> > Starting with libcap v2.60 (commit aca076443591 "Make cap_t operations
> > thread safe"), a '__u8 mutex' was added to the beginning of the internal
> > 'struct _cap_struct'. This structural shift breaks the selftest's custom
> > struct layout assumptions, causing capability corruption and leading to
> > failures during test execution.
> > 
> > Fix this by removing the custom struct definition and use the standard
> > cap_set_flag() API.
> 
> What king of failures are you seeing? Can you send the failures from
> the run?
> 

I can reproduce the failure on CentOS Stream 10 with libcap 2.69:

TAP version 13
1..1
# Starting 1 tests from 1 test cases.
#  RUN           global.clone3_cap_checkpoint_restore ...
# clone3() syscall supported
# clone3_cap_checkpoint_restore.c:151:clone3_cap_checkpoint_restore:Child has PID 10647
cap_set_proc: Operation not permitted
# clone3_cap_checkpoint_restore.c:160:clone3_cap_checkpoint_restore:Expected set_capability() (-1) == 0 (0)
# clone3_cap_checkpoint_restore.c:161:clone3_cap_checkpoint_restore:Could not set CAP_CHECKPOINT_RESTORE
# clone3_cap_checkpoint_restore: Test terminated by assertion
#          FAIL  global.clone3_cap_checkpoint_restore
not ok 1 global.clone3_cap_checkpoint_restore
# FAILED: 0 / 1 tests passed.
# Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0

After applying the proposed fix patch that uses standard libcap APIs,
the test passes in my test environment. It also passes on CentOS Stream
9 with libcap 2.48 (<2.60).

Regards,
Yi Lai

> > 
> > Fixes: 1d27a0be16d6 ("selftests: add clone3() CAP_CHECKPOINT_RESTORE test")
> > Signed-off-by: Yi Lai <yi1.lai@intel.com>
> > ---
> 
> thanks,
> -- Shuah

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-01  1:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10  3:14 [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API Yi Lai
2026-03-31 20:10 ` Shuah Khan
2026-04-01  1:11   ` Lai, Yi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox