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