1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2001-2021 4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 5f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Verify that, fchown(2) succeeds to change the group of a file specified 12f08c3bdfSopenharmony_ci * by path when called by non-root user with the following constraints: 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * - euid of the process is equal to the owner of the file 15f08c3bdfSopenharmony_ci * - the intended gid is either egid, or one of the supplementary gids 16f08c3bdfSopenharmony_ci * of the process. 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * Also verify that fchown() clears the setuid/setgid bits set on the file. 19f08c3bdfSopenharmony_ci */ 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include <grp.h> 22f08c3bdfSopenharmony_ci#include <pwd.h> 23f08c3bdfSopenharmony_ci#include <fcntl.h> 24f08c3bdfSopenharmony_ci#include <sys/stat.h> 25f08c3bdfSopenharmony_ci#include <sys/types.h> 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#include "tst_test.h" 28f08c3bdfSopenharmony_ci#include "compat_tst_16.h" 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 31f08c3bdfSopenharmony_ci#define NEW_PERMS (S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID) 32f08c3bdfSopenharmony_ci#define FILENAME "fchown03_testfile" 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic int fd; 35f08c3bdfSopenharmony_cistatic struct passwd *ltpuser; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic void check_owner(struct stat *s, uid_t exp_uid, gid_t exp_gid) 38f08c3bdfSopenharmony_ci{ 39f08c3bdfSopenharmony_ci if (s->st_uid != exp_uid || s->st_gid != exp_gid) 40f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: wrong owner set to (uid=%d, gid=%d), expected (uid=%d, gid=%d)", 41f08c3bdfSopenharmony_ci FILENAME, s->st_uid, s->st_gid, exp_uid, exp_gid); 42f08c3bdfSopenharmony_ci} 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void check_mode(struct stat *s, mode_t exp_mode) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci if (s->st_mode != exp_mode) 47f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: wrong mode permissions %#o, expected %#o", 48f08c3bdfSopenharmony_ci FILENAME, s->st_mode, exp_mode); 49f08c3bdfSopenharmony_ci} 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic void run(void) 52f08c3bdfSopenharmony_ci{ 53f08c3bdfSopenharmony_ci uid_t uid; 54f08c3bdfSopenharmony_ci gid_t gid; 55f08c3bdfSopenharmony_ci struct stat stat_buf; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci SAFE_SETEUID(0); 58f08c3bdfSopenharmony_ci SAFE_FCHOWN(fd, -1, 0); 59f08c3bdfSopenharmony_ci SAFE_FCHMOD(fd, NEW_PERMS); 60f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci UID16_CHECK((uid = geteuid()), "fchown"); 63f08c3bdfSopenharmony_ci GID16_CHECK((gid = getegid()), "fchown"); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci SAFE_STAT(FILENAME, &stat_buf); 66f08c3bdfSopenharmony_ci check_owner(&stat_buf, uid, 0); 67f08c3bdfSopenharmony_ci check_mode(&stat_buf, NEW_PERMS); 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci TST_EXP_PASS(FCHOWN(fd, -1, gid), "fchown(fd, %d, %d)", -1, gid); 70f08c3bdfSopenharmony_ci SAFE_STAT(FILENAME, &stat_buf); 71f08c3bdfSopenharmony_ci check_owner(&stat_buf, uid, gid); 72f08c3bdfSopenharmony_ci check_mode(&stat_buf, NEW_PERMS & ~(S_ISUID | S_ISGID)); 73f08c3bdfSopenharmony_ci} 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_cistatic void setup(void) 76f08c3bdfSopenharmony_ci{ 77f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM("nobody"); 78f08c3bdfSopenharmony_ci SAFE_SETEGID(ltpuser->pw_gid); 79f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, FILE_MODE); 82f08c3bdfSopenharmony_ci} 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_cistatic void cleanup(void) 85f08c3bdfSopenharmony_ci{ 86f08c3bdfSopenharmony_ci SAFE_SETEGID(0); 87f08c3bdfSopenharmony_ci SAFE_SETEUID(0); 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci if (fd > 0) 90f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 91f08c3bdfSopenharmony_ci} 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_cistatic struct tst_test test = { 94f08c3bdfSopenharmony_ci .needs_root = 1, 95f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 96f08c3bdfSopenharmony_ci .setup = setup, 97f08c3bdfSopenharmony_ci .cleanup = cleanup, 98f08c3bdfSopenharmony_ci .test_all = run, 99f08c3bdfSopenharmony_ci}; 100