1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 07/2001 John George 5 * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com> 6 */ 7 8/*\ 9 * [Description] 10 * 11 * Verify that system call utime() fails with 12 * 13 * - EACCES when times argument is NULL and user does not have rights to modify 14 * the file. 15 * - ENOENT when specified file does not exist. 16 * - EPERM when times argument is not NULL and user does not have rights to 17 * modify the file. 18 * - EROFS when the path resides on a read-only filesystem. 19 */ 20 21#include <pwd.h> 22#include <utime.h> 23 24#include "tst_test.h" 25 26#define TEMP_FILE "tmp_file" 27#define MNT_POINT "mntpoint" 28#define FILE_MODE 0644 29#define TEST_USERNAME "nobody" 30 31static const struct utimbuf times; 32 33static struct tcase { 34 char *pathname; 35 int exp_errno; 36 const struct utimbuf *utimbuf; 37 char *err_desc; 38} tcases[] = { 39 {TEMP_FILE, EACCES, NULL, "No write access"}, 40 {"", ENOENT, NULL, "File not exist"}, 41 {TEMP_FILE, EPERM, ×, "Not file owner"}, 42 {MNT_POINT, EROFS, NULL, "Read-only filesystem"} 43}; 44 45 46static void setup(void) 47{ 48 struct passwd *pw; 49 50 SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL); 51 52 pw = SAFE_GETPWNAM(TEST_USERNAME); 53 tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name); 54 SAFE_SETEUID(pw->pw_uid); 55} 56 57static void run(unsigned int i) 58{ 59 struct tcase *tc = &tcases[i]; 60 61 TST_EXP_FAIL(utime(tc->pathname, tc->utimbuf), 62 tc->exp_errno, "%s", tc->err_desc); 63} 64 65static struct tst_test test = { 66 .setup = setup, 67 .test = run, 68 .tcnt = ARRAY_SIZE(tcases), 69 .needs_root = 1, 70 .needs_tmpdir = 1, 71 .mntpoint = MNT_POINT, 72 .needs_rofs = 1 73}; 74