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 * Test Name: chmod07 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * Test Description: 10f08c3bdfSopenharmony_ci * Verify that, chmod(2) will succeed to change the mode of a file/directory 11f08c3bdfSopenharmony_ci * and sets the sticky bit on it if invoked by root (uid = 0) process with 12f08c3bdfSopenharmony_ci * the following constraints, 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 * Expected Result: 18f08c3bdfSopenharmony_ci * chmod() should return value 0 on success and succeeds to set sticky bit 19f08c3bdfSopenharmony_ci * on the specified file. 20f08c3bdfSopenharmony_ci */ 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include <stdio.h> 23f08c3bdfSopenharmony_ci#include <sys/types.h> 24f08c3bdfSopenharmony_ci#include <sys/stat.h> 25f08c3bdfSopenharmony_ci#include <fcntl.h> 26f08c3bdfSopenharmony_ci#include <errno.h> 27f08c3bdfSopenharmony_ci#include <string.h> 28f08c3bdfSopenharmony_ci#include <signal.h> 29f08c3bdfSopenharmony_ci#include <grp.h> 30f08c3bdfSopenharmony_ci#include <pwd.h> 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci#include "tst_test.h" 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 35f08c3bdfSopenharmony_ci#define PERMS 01777 /* Permissions with sticky bit set. */ 36f08c3bdfSopenharmony_ci#define TESTFILE "testfile" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_civoid test_chmod(void) 39f08c3bdfSopenharmony_ci{ 40f08c3bdfSopenharmony_ci struct stat stat_buf; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci /* 43f08c3bdfSopenharmony_ci * Call chmod(2) with specified mode argument 44f08c3bdfSopenharmony_ci * (sticky-bit set) on testfile. 45f08c3bdfSopenharmony_ci */ 46f08c3bdfSopenharmony_ci TEST(chmod(TESTFILE, PERMS)); 47f08c3bdfSopenharmony_ci if (TST_RET == -1) { 48f08c3bdfSopenharmony_ci tst_brk(TFAIL | TTERRNO, "chmod(%s, %#o) failed", 49f08c3bdfSopenharmony_ci TESTFILE, PERMS); 50f08c3bdfSopenharmony_ci } 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci if (stat(TESTFILE, &stat_buf) == -1) { 53f08c3bdfSopenharmony_ci tst_brk(TFAIL | TTERRNO, "stat failed"); 54f08c3bdfSopenharmony_ci } 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci /* Check for expected mode permissions */ 57f08c3bdfSopenharmony_ci if ((stat_buf.st_mode & PERMS) == PERMS) { 58f08c3bdfSopenharmony_ci tst_res(TPASS, "Functionality of chmod(%s, %#o) successful", 59f08c3bdfSopenharmony_ci TESTFILE, PERMS); 60f08c3bdfSopenharmony_ci } else { 61f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: Incorrect modes 0%03o; expected 0%03o", 62f08c3bdfSopenharmony_ci TESTFILE, stat_buf.st_mode, PERMS); 63f08c3bdfSopenharmony_ci } 64f08c3bdfSopenharmony_ci} 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_civoid setup(void) 67f08c3bdfSopenharmony_ci{ 68f08c3bdfSopenharmony_ci struct passwd *ltpuser; 69f08c3bdfSopenharmony_ci struct group *ltpgroup; 70f08c3bdfSopenharmony_ci int fd; 71f08c3bdfSopenharmony_ci gid_t group1_gid; 72f08c3bdfSopenharmony_ci uid_t user1_uid; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM("nobody"); 75f08c3bdfSopenharmony_ci user1_uid = ltpuser->pw_uid; 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon"); 78f08c3bdfSopenharmony_ci group1_gid = ltpgroup->gr_gid; 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); 81f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 82f08c3bdfSopenharmony_ci SAFE_CHOWN(TESTFILE, user1_uid, group1_gid); 83f08c3bdfSopenharmony_ci SAFE_SETGID(group1_gid); 84f08c3bdfSopenharmony_ci} 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_cistatic struct tst_test test = { 87f08c3bdfSopenharmony_ci .needs_root = 1, 88f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 89f08c3bdfSopenharmony_ci .setup = setup, 90f08c3bdfSopenharmony_ci .test_all = test_chmod, 91f08c3bdfSopenharmony_ci}; 92