1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 4f08c3bdfSopenharmony_ci * Author: Saji Kumar.V.R <saji.kumar@wipro.com> 5f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2003-2023 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * Tests basic error handling of the capset syscall. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * 1. capset() fails with errno set to EFAULT if an invalid address 13f08c3bdfSopenharmony_ci * is given for header. 14f08c3bdfSopenharmony_ci * 2. capset() fails with errno set to EFAULT if an invalid address 15f08c3bdfSopenharmony_ci * is given for data. 16f08c3bdfSopenharmony_ci * 3. capset() fails with errno set to EINVAL if an invalid value 17f08c3bdfSopenharmony_ci * is given for header->version. 18f08c3bdfSopenharmony_ci * 4. capset() fails with errno set to EPERM if the new_Effective is 19f08c3bdfSopenharmony_ci * not a subset of the new_Permitted. 20f08c3bdfSopenharmony_ci * 5. capset() fails with errno set to EPERM if the new_Permitted is 21f08c3bdfSopenharmony_ci * not a subset of the old_Permitted. 22f08c3bdfSopenharmony_ci * 6. capset() fails with errno set ot EPERM if the new_Inheritable is 23f08c3bdfSopenharmony_ci * not a subset of the old_Inheritable and bounding set. 24f08c3bdfSopenharmony_ci */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#include <stdlib.h> 27f08c3bdfSopenharmony_ci#include <sys/types.h> 28f08c3bdfSopenharmony_ci#include <unistd.h> 29f08c3bdfSopenharmony_ci#include <sys/prctl.h> 30f08c3bdfSopenharmony_ci#include "tst_test.h" 31f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 32f08c3bdfSopenharmony_ci#include <linux/capability.h> 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci#define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN | 1 << CAP_SETPCAP) 35f08c3bdfSopenharmony_ci#define CAP2 (CAP1 | 1 << CAP_KILL) 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic struct __user_cap_header_struct *header; 38f08c3bdfSopenharmony_cistatic struct __user_cap_data_struct *data; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic void *bad_addr; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic struct tcase { 43f08c3bdfSopenharmony_ci int version; 44f08c3bdfSopenharmony_ci int pid; 45f08c3bdfSopenharmony_ci int effective; 46f08c3bdfSopenharmony_ci int permitted; 47f08c3bdfSopenharmony_ci int inheritable; 48f08c3bdfSopenharmony_ci int exp_err; 49f08c3bdfSopenharmony_ci int flag; 50f08c3bdfSopenharmony_ci char *message; 51f08c3bdfSopenharmony_ci} tcases[] = { 52f08c3bdfSopenharmony_ci {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "bad address header"}, 53f08c3bdfSopenharmony_ci {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "bad address data"}, 54f08c3bdfSopenharmony_ci {0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "bad version"}, 55f08c3bdfSopenharmony_ci {0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "bad value data(when pE is not in pP)"}, 56f08c3bdfSopenharmony_ci {0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "bad value data(when pP is not in old pP)"}, 57f08c3bdfSopenharmony_ci {0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "bad value data(when pI is not in bounding set or old pI)"}, 58f08c3bdfSopenharmony_ci}; 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void verify_capset(unsigned int n) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci header->version = tc->version; 65f08c3bdfSopenharmony_ci header->pid = tc->pid; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci data->effective = tc->effective; 68f08c3bdfSopenharmony_ci data->permitted = tc->permitted; 69f08c3bdfSopenharmony_ci data->inheritable = tc->inheritable; 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci TST_EXP_FAIL(tst_syscall(__NR_capset, tc->flag - 1 ? header : bad_addr, 72f08c3bdfSopenharmony_ci tc->flag - 2 ? data : bad_addr), 73f08c3bdfSopenharmony_ci tc->exp_err, "capset() with %s", tc->message); 74f08c3bdfSopenharmony_ci /* 75f08c3bdfSopenharmony_ci * When an unsupported version value is specified, it will 76f08c3bdfSopenharmony_ci * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?. 77f08c3bdfSopenharmony_ci * Since linux 2.6.26, version 3 is default. We use it. 78f08c3bdfSopenharmony_ci */ 79f08c3bdfSopenharmony_ci if (header->version != 0x20080522) 80f08c3bdfSopenharmony_ci tst_res(TFAIL, "kernel doesn't return preferred linux" 81f08c3bdfSopenharmony_ci " capability version when using bad version"); 82f08c3bdfSopenharmony_ci} 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_cistatic void setup(void) 85f08c3bdfSopenharmony_ci{ 86f08c3bdfSopenharmony_ci header->version = 0x20080522; 87f08c3bdfSopenharmony_ci data->effective = CAP1; 88f08c3bdfSopenharmony_ci data->permitted = CAP1; 89f08c3bdfSopenharmony_ci data->inheritable = CAP1; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci TEST(tst_syscall(__NR_capset, header, data)); 92f08c3bdfSopenharmony_ci if (TST_RET == -1) 93f08c3bdfSopenharmony_ci tst_brk(TBROK | TTERRNO, "capset data failed"); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci TEST(prctl(PR_CAPBSET_DROP, CAP_KILL)); 96f08c3bdfSopenharmony_ci if (TST_RET == -1) 97f08c3bdfSopenharmony_ci tst_brk(TBROK | TTERRNO, "drop CAP_KILL failed"); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci bad_addr = tst_get_bad_addr(NULL); 100f08c3bdfSopenharmony_ci} 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_cistatic struct tst_test test = { 103f08c3bdfSopenharmony_ci .setup = setup, 104f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 105f08c3bdfSopenharmony_ci .test = verify_capset, 106f08c3bdfSopenharmony_ci .needs_root = 1, 107f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 108f08c3bdfSopenharmony_ci {&header, .size = sizeof(*header)}, 109f08c3bdfSopenharmony_ci {&data, .size = 2 * sizeof(*data)}, 110f08c3bdfSopenharmony_ci {}, 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci}; 113