From nobody Wed Nov 27 00:36:58 2024 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7ABD9140E50; Tue, 15 Oct 2024 11:01:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728990069; cv=none; b=Zr07S2Ybqqfk1/ndoUHjnrGkQ4+CjOnxz8GyJPUFCzXyz4hJrQ+UrzEq81wbu2wYCVQZmSn3fagslI6eUygSgLLRhidFTU7bNsqszkYm/I85VN6SCQ7oNHAH5eapHifdUV/D2ieF6Ak6ZRmYB3M06q7v1WLVtP5rPMw/hIKlx+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728990069; c=relaxed/simple; bh=WB6mgSkA81g3/KEMsbSRyW2k533ipwlnb4WNkoNtomQ=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=eQstQua2tUHjmPqNQPEAjyk3TZzHMV0WJy0hemgcQp24yrRfHtcpbLf0iZMZmbHejVy1XGE0XlXCi2tVFRM9TukJS05SOIvf2L/9SoYMsz8ZfQaCiiLssU/IjMzlcv5vPHCaH9rneOOPPK4XmIc4SI117fumpX0pCOyHj8GBODM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=XK1hrJZl; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="XK1hrJZl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=cKk7J UQzl8vCMU2KIrJzQardE31VHcQXn8CelcJsQsc=; b=XK1hrJZlt9+w3WlkkdJPL +8imNMUKJ2WDpj9XaMG8m4rRcJffRN22Z1CZNl6uzhKJ+6hOgXznoZN8gjwsfIaA ruuTrVOT1nSIjI5F/2Q/Tmlw1+IehBgVnGRFNBvWcQLNodRzkaQGatBUK9Zp7BQd AqbxEI88O/zWTTSzj434CI= Received: from localhost.localdomain (unknown [111.48.69.246]) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3n+pgSw5nWa8pBQ--.29774S2; Tue, 15 Oct 2024 19:00:50 +0800 (CST) From: zhouyuhang To: brauner@kernel.org, shuah@kernel.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhouyuhang Subject: [PATCH v2] selftests: clone3: Use the capget and capset syscall directly Date: Tue, 15 Oct 2024 18:59:54 +0800 Message-Id: <20241015105955.126994-1-zhouyuhang1010@163.com> X-Mailer: git-send-email 2.27.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wD3n+pgSw5nWa8pBQ--.29774S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxJw4kWr4kGFW3AFykCw4rZrb_yoWruFyfpa 48Zr45KFs5Wr1xGFW8AwsruF1rKF4kXF48Jr1UJw1j9r13Krn2yr4IkFykK3W2g3yDu3y5 ua10gF4SvF1kJ3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07bjpndUUUUU= X-CM-SenderInfo: 52kr35xxkd0warqriqqrwthudrp/1tbiLwd5JmcOLWIswAABs8 Content-Type: text/plain; charset="utf-8" From: zhouyuhang The libcap commit aca076443591 ("Make cap_t operations thread safe.") added a __u8 mutex at the beginning of the struct _cap_struct, it changes the offset of the members in the structure that breaks the assumption made in the "struct libcap" definition in clone3_cap_checkpoint_restore.c. This will make the test fail. So use the capget and capset syscall directly and remove the libcap library dependency like the commit 663af70aabb7 ("bpf: selftests: Add helpers to directly use the capget and capset syscall") does. Signed-off-by: zhouyuhang --- tools/testing/selftests/clone3/Makefile | 1 - .../clone3/clone3_cap_checkpoint_restore.c | 53 ++++++++----------- .../selftests/clone3/clone3_cap_helpers.h | 23 ++++++++ 3 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/clone3/clone3_cap_helpers.h diff --git a/tools/testing/selftests/clone3/Makefile b/tools/testing/selfte= sts/clone3/Makefile index 84832c369a2e..59d26e8da8d2 100644 --- a/tools/testing/selftests/clone3/Makefile +++ b/tools/testing/selftests/clone3/Makefile @@ -1,6 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS +=3D -g -std=3Dgnu99 $(KHDR_INCLUDES) -LDLIBS +=3D -lcap =20 TEST_GEN_PROGS :=3D clone3 clone3_clear_sighand clone3_set_tid \ clone3_cap_checkpoint_restore diff --git a/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c= b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c index 3c196fa86c99..242088eeec88 100644 --- a/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c +++ b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -26,6 +25,7 @@ =20 #include "../kselftest_harness.h" #include "clone3_selftests.h" +#include "clone3_cap_helpers.h" =20 static void child_exit(int ret) { @@ -87,47 +87,36 @@ static int test_clone3_set_tid(struct __test_metadata *= _metadata, return ret; } =20 -struct libcap { - struct __user_cap_header_struct hdr; - struct __user_cap_data_struct data[2]; -}; - static int set_capability(void) { - cap_value_t cap_values[] =3D { CAP_SETUID, CAP_SETGID }; - struct libcap *cap; - int ret =3D -1; - cap_t caps; - - caps =3D cap_get_proc(); - if (!caps) { - perror("cap_get_proc"); + struct __user_cap_data_struct data[2]; + struct __user_cap_header_struct hdr =3D { + .version =3D _LINUX_CAPABILITY_VERSION_3, + }; + __u32 cap0 =3D 1 << CAP_SETUID | 1 << CAP_SETGID; + __u32 cap1 =3D 1 << (CAP_CHECKPOINT_RESTORE - 32); + int ret; + + ret =3D capget(&hdr, data); + if (ret) { + perror("capget"); return -1; } =20 /* Drop all capabilities */ - if (cap_clear(caps)) { - perror("cap_clear"); - goto out; - } + memset(&data, 0, sizeof(data)); =20 - cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET); - cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET); + data[0].effective |=3D cap0; + data[0].permitted |=3D cap0; =20 - cap =3D (struct libcap *) caps; + data[1].effective |=3D cap1; + data[1].permitted |=3D cap1; =20 - /* 40 -> CAP_CHECKPOINT_RESTORE */ - cap->data[1].effective |=3D 1 << (40 - 32); - cap->data[1].permitted |=3D 1 << (40 - 32); - - if (cap_set_proc(caps)) { - perror("cap_set_proc"); - goto out; + ret =3D capset(&hdr, data); + if (ret) { + perror("capset"); + return -1; } - ret =3D 0; -out: - if (cap_free(caps)) - perror("cap_free"); return ret; } =20 diff --git a/tools/testing/selftests/clone3/clone3_cap_helpers.h b/tools/te= sting/selftests/clone3/clone3_cap_helpers.h new file mode 100644 index 000000000000..3fa59ef68fb8 --- /dev/null +++ b/tools/testing/selftests/clone3/clone3_cap_helpers.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __CLONE3_CAP_HELPERS_H +#define __CLONE3_CAP_HELPERS_H + +#include + +/* + * Compatible with older version + * header file without defined + * CAP_CHECKPOINT_RESTORE. + */ +#ifndef CAP_CHECKPOINT_RESTORE +#define CAP_CHECKPOINT_RESTORE 40 +#endif + +/* + * Removed the libcap library dependency. + * So declare them here directly. + */ +int capget(cap_user_header_t header, cap_user_data_t data); +int capset(cap_user_header_t header, const cap_user_data_t data); + +#endif --=20 2.27.0