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 10f08c3bdfSopenharmony_ci * and set the sticky bit on it if invoked by non-root (uid != 0) 11f08c3bdfSopenharmony_ci * process with the following constraints: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - the process is the owner of the file 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 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include <pwd.h> 19f08c3bdfSopenharmony_ci#include "fchmod.h" 20f08c3bdfSopenharmony_ci#include "tst_test.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_cistatic int fd; 23f08c3bdfSopenharmony_cistatic const char nobody_uid[] = "nobody"; 24f08c3bdfSopenharmony_cistatic struct passwd *ltpuser; 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_cistatic void verify_fchmod(void) 27f08c3bdfSopenharmony_ci{ 28f08c3bdfSopenharmony_ci struct stat stat_buf; 29f08c3bdfSopenharmony_ci mode_t file_mode; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci TST_EXP_PASS_SILENT(fchmod(fd, PERMS)); 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci if (fstat(fd, &stat_buf) == -1) 34f08c3bdfSopenharmony_ci tst_brk(TFAIL | TERRNO, "fstat failed"); 35f08c3bdfSopenharmony_ci file_mode = stat_buf.st_mode; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci if ((file_mode & PERMS) != PERMS) 38f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: Incorrect modes 0%3o, " 39f08c3bdfSopenharmony_ci "Expected 0777", TESTFILE, file_mode); 40f08c3bdfSopenharmony_ci else 41f08c3bdfSopenharmony_ci tst_res(TPASS, "Functionality of fchmod(%d, " 42f08c3bdfSopenharmony_ci "%#o) successful", fd, PERMS); 43f08c3bdfSopenharmony_ci} 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void setup(void) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM(nobody_uid); 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void cleanup(void) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci if (fd > 0) 57f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 58f08c3bdfSopenharmony_ci} 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic struct tst_test test = { 61f08c3bdfSopenharmony_ci .test_all = verify_fchmod, 62f08c3bdfSopenharmony_ci .setup = setup, 63f08c3bdfSopenharmony_ci .cleanup = cleanup, 64f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 65f08c3bdfSopenharmony_ci .needs_root = 1, 66f08c3bdfSopenharmony_ci}; 67