1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * Test Description: 6 * Verify that the system call stime() fails to set the system's idea 7 * of data and time if invoked by "non-root" user. 8 * 9 * Expected Result: 10 * stime() should fail with return value -1 and set errno to EPERM. 11 * 12 * History 13 * 07/2001 John George 14 * -Ported 15 */ 16 17#include <sys/types.h> 18#include <errno.h> 19#include <time.h> 20#include <pwd.h> 21 22#include "tst_test.h" 23#include "stime_var.h" 24 25static time_t new_time; 26 27static void run(void) 28{ 29 TEST(do_stime(&new_time)); 30 if (TST_RET != -1) { 31 tst_res(TFAIL, 32 "stime() returned %ld, expected -1 EPERM", TST_RET); 33 return; 34 } 35 36 if (TST_ERR == EPERM) { 37 tst_res(TPASS | TTERRNO, "stime(2) fails, Caller not root"); 38 return; 39 } 40 41 tst_res(TFAIL | TTERRNO, 42 "stime(2) fails, Caller not root, expected errno:%d", EPERM); 43} 44 45static void setup(void) 46{ 47 time_t curr_time; 48 struct passwd *ltpuser; 49 50 stime_info(); 51 52 ltpuser = SAFE_GETPWNAM("nobody"); 53 SAFE_SETUID(ltpuser->pw_uid); 54 55 if ((curr_time = time(NULL)) < 0) 56 tst_brk(TBROK | TERRNO, "time() failed"); 57 58 new_time = curr_time + 10; 59} 60 61static struct tst_test test = { 62 .test_all = run, 63 .setup = setup, 64 .needs_root = 1, 65 .test_variants = TEST_VARIANTS, 66}; 67