public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Yi Lai <yi1.lai@intel.com>
To: brauner@kernel.org, shuah@kernel.org, areber@redhat.com,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	yi1.lai@intel.com, yi1.lai@linux.intel.com
Subject: [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API
Date: Tue, 10 Mar 2026 11:14:15 +0800	[thread overview]
Message-ID: <20260310031415.151531-1-yi1.lai@intel.com> (raw)

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


             reply	other threads:[~2026-03-10  3:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10  3:14 Yi Lai [this message]
2026-03-31 20:10 ` [PATCH] selftests/clone3: Fix capability dropping by using standard libcap API Shuah Khan
2026-04-01  1:11   ` Lai, Yi
2026-05-06  8:16     ` Lai, Yi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260310031415.151531-1-yi1.lai@intel.com \
    --to=yi1.lai@intel.com \
    --cc=areber@redhat.com \
    --cc=brauner@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=yi1.lai@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox