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, chmod(2) will succeed to change the mode of a file or directory 11f08c3bdfSopenharmony_ci * and set the sticky bit on it if invoked by non-root (uid != 0) 12f08c3bdfSopenharmony_ci * process with the following constraints: 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * - the process is the owner of the file or directory. 15f08c3bdfSopenharmony_ci * - the effective group ID or one of the supplementary group ID's of the 16f08c3bdfSopenharmony_ci * process is equal to the group ID of the file or directory. 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#include <pwd.h> 20f08c3bdfSopenharmony_ci#include "tst_test.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 23f08c3bdfSopenharmony_ci#define DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO 24f08c3bdfSopenharmony_ci#define PERMS 01777 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define TESTFILE "testfile" 27f08c3bdfSopenharmony_ci#define TESTDIR "testdir_3" 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic struct tcase { 30f08c3bdfSopenharmony_ci char *name; 31f08c3bdfSopenharmony_ci char *desc; 32f08c3bdfSopenharmony_ci} tcases[] = { 33f08c3bdfSopenharmony_ci {TESTFILE, "verify permissions of file"}, 34f08c3bdfSopenharmony_ci {TESTDIR, "verify permissions of directory"}, 35f08c3bdfSopenharmony_ci}; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic void verify_chmod(unsigned int n) 38f08c3bdfSopenharmony_ci{ 39f08c3bdfSopenharmony_ci struct stat stat_buf; 40f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci TST_EXP_PASS(chmod(tc->name, PERMS), "chmod(%s, %04o)", 43f08c3bdfSopenharmony_ci tc->name, PERMS); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci if (!TST_PASS) 46f08c3bdfSopenharmony_ci return; 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci SAFE_STAT(tc->name, &stat_buf); 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci if ((stat_buf.st_mode & PERMS) != PERMS) { 51f08c3bdfSopenharmony_ci tst_res(TFAIL, "stat(%s) mode=%04o", 52f08c3bdfSopenharmony_ci tc->name, stat_buf.st_mode); 53f08c3bdfSopenharmony_ci } else { 54f08c3bdfSopenharmony_ci tst_res(TPASS, "stat(%s) mode=%04o", 55f08c3bdfSopenharmony_ci tc->name, stat_buf.st_mode); 56f08c3bdfSopenharmony_ci } 57f08c3bdfSopenharmony_ci} 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic void setup(void) 60f08c3bdfSopenharmony_ci{ 61f08c3bdfSopenharmony_ci char nobody_uid[] = "nobody"; 62f08c3bdfSopenharmony_ci struct passwd *ltpuser; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM(nobody_uid); 65f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci SAFE_TOUCH(TESTFILE, FILE_MODE, NULL); 68f08c3bdfSopenharmony_ci SAFE_MKDIR(TESTDIR, DIR_MODE); 69f08c3bdfSopenharmony_ci} 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_cistatic struct tst_test test = { 72f08c3bdfSopenharmony_ci .setup = setup, 73f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 74f08c3bdfSopenharmony_ci .test = verify_chmod, 75f08c3bdfSopenharmony_ci .needs_root = 1, 76f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 77f08c3bdfSopenharmony_ci}; 78