1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. 4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2020-2023 5f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * capset() fails with errno set or EPERM if the new_Inheritable is 12f08c3bdfSopenharmony_ci * not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP. 13f08c3bdfSopenharmony_ci */ 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#include <stdlib.h> 16f08c3bdfSopenharmony_ci#include <sys/types.h> 17f08c3bdfSopenharmony_ci#include <unistd.h> 18f08c3bdfSopenharmony_ci#include "tst_test.h" 19f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 20f08c3bdfSopenharmony_ci#include <linux/capability.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#define CAP1 (1 << CAP_KILL) 23f08c3bdfSopenharmony_ci#define CAP2 (CAP1 | 1 << CAP_NET_RAW) 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic struct __user_cap_header_struct *header; 26f08c3bdfSopenharmony_cistatic struct __user_cap_data_struct *data; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic void verify_capset(void) 29f08c3bdfSopenharmony_ci{ 30f08c3bdfSopenharmony_ci tst_res(TINFO, "Test bad value data(when pI is not old pP or old pI without CAP_SETPCAP)"); 31f08c3bdfSopenharmony_ci data[0].inheritable = CAP2; 32f08c3bdfSopenharmony_ci TST_EXP_FAIL(tst_syscall(__NR_capset, header, data), EPERM, "capset()"); 33f08c3bdfSopenharmony_ci} 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic void setup(void) 36f08c3bdfSopenharmony_ci{ 37f08c3bdfSopenharmony_ci header->version = 0x20080522; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci data[0].effective = CAP1; 40f08c3bdfSopenharmony_ci data[0].permitted = CAP1; 41f08c3bdfSopenharmony_ci data[0].inheritable = CAP1; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci TEST(tst_syscall(__NR_capset, header, data)); 44f08c3bdfSopenharmony_ci if (TST_RET == -1) 45f08c3bdfSopenharmony_ci tst_brk(TBROK | TTERRNO, "capset data failed"); 46f08c3bdfSopenharmony_ci} 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_cistatic struct tst_test test = { 49f08c3bdfSopenharmony_ci .setup = setup, 50f08c3bdfSopenharmony_ci .test_all = verify_capset, 51f08c3bdfSopenharmony_ci .needs_root = 1, 52f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 53f08c3bdfSopenharmony_ci {&header, .size = sizeof(*header)}, 54f08c3bdfSopenharmony_ci {&data, .size = 2 * sizeof(*data)}, 55f08c3bdfSopenharmony_ci {}, 56f08c3bdfSopenharmony_ci } 57f08c3bdfSopenharmony_ci}; 58