1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/*\ 7f08c3bdfSopenharmony_ci * [Description] 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * Verify that, fchmod(2) will succeed to change the mode of a file/directory 10f08c3bdfSopenharmony_ci * set the sticky bit on it if invoked by root (uid = 0) process with 11f08c3bdfSopenharmony_ci * the following constraints: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - the process is not the owner of the file/directory 14f08c3bdfSopenharmony_ci * - the effective group ID or one of the supplementary group ID's of the 15f08c3bdfSopenharmony_ci * process is equal to the group ID of the file/directory 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <pwd.h> 19f08c3bdfSopenharmony_ci#include <grp.h> 20f08c3bdfSopenharmony_ci#include <errno.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci#include "fchmod.h" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic int fd; 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic void verify_fchmod(void) 28f08c3bdfSopenharmony_ci{ 29f08c3bdfSopenharmony_ci struct stat stat_buf; 30f08c3bdfSopenharmony_ci mode_t file_mode; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci TEST(fchmod(fd, PERMS)); 33f08c3bdfSopenharmony_ci if (TST_RET == -1) 34f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly"); 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &stat_buf); 37f08c3bdfSopenharmony_ci file_mode = stat_buf.st_mode; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci if ((file_mode & ~S_IFREG) != PERMS) { 40f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: Incorrect modes 0%03o, Expected 0%03o", 41f08c3bdfSopenharmony_ci TESTFILE, file_mode, PERMS); 42f08c3bdfSopenharmony_ci } else { 43f08c3bdfSopenharmony_ci tst_res(TPASS, "Functionality of fchmod(%d, %#o) Successful", 44f08c3bdfSopenharmony_ci fd, PERMS); 45f08c3bdfSopenharmony_ci } 46f08c3bdfSopenharmony_ci} 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_cistatic void setup(void) 49f08c3bdfSopenharmony_ci{ 50f08c3bdfSopenharmony_ci struct passwd *ltpuser; 51f08c3bdfSopenharmony_ci struct group *ltpgroup; 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM("nobody"); 54f08c3bdfSopenharmony_ci ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon"); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); 57f08c3bdfSopenharmony_ci SAFE_CHOWN(TESTFILE, ltpuser->pw_uid, ltpgroup->gr_gid); 58f08c3bdfSopenharmony_ci SAFE_SETGID(ltpgroup->gr_gid); 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic void cleanup(void) 62f08c3bdfSopenharmony_ci{ 63f08c3bdfSopenharmony_ci if (fd > 0) 64f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 65f08c3bdfSopenharmony_ci} 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_cistatic struct tst_test test = { 68f08c3bdfSopenharmony_ci .test_all = verify_fchmod, 69f08c3bdfSopenharmony_ci .needs_root = 1, 70f08c3bdfSopenharmony_ci .setup = setup, 71f08c3bdfSopenharmony_ci .cleanup = cleanup, 72f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 73f08c3bdfSopenharmony_ci}; 74