blob: 41c69ec89c8aab948cf4ca19fad9979cc235e413 [file] [log] [blame]
robbiewd62a36d2002-07-08 16:40:03 +00001/*
robbiewd62a36d2002-07-08 16:40:03 +00002 * Copyright (C) Bull S.A. 2001
3 * Copyright (c) International Business Machines Corp., 2001
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +01004 * 06/2002 Ported by Jacky Malcles
5 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
robbiewd62a36d2002-07-08 16:40:03 +00006 *
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
robbiewd62a36d2002-07-08 16:40:03 +000011 *
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU General Public License for more details.
robbiewd62a36d2002-07-08 16:40:03 +000016 *
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewd62a36d2002-07-08 16:40:03 +000020 */
21
22/*
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010023 * Verify that, link() fails with -1 and sets errno to EACCES when Write access
24 * to the directory containing newpath is not allowed for the process's
25 * effective uid.
robbiewd62a36d2002-07-08 16:40:03 +000026 */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010027
robbiewd62a36d2002-07-08 16:40:03 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <errno.h>
33#include <string.h>
34#include <signal.h>
35#include <sys/types.h>
36#include <sys/stat.h>
Garrett Cooper91ec53d2010-03-07 14:19:11 -080037#include <pwd.h>
robbiewd62a36d2002-07-08 16:40:03 +000038
39#include "test.h"
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010040#include "safe_macros.h"
robbiewd62a36d2002-07-08 16:40:03 +000041
robbiewd62a36d2002-07-08 16:40:03 +000042#define NOBODY_USER 99
43#define MODE_TO S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH
44
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010045static void setup(void);
46static void cleanup(void);
robbiewd62a36d2002-07-08 16:40:03 +000047
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020048char *TCID = "link06";
49int TST_TOTAL = 1;
robbiewd62a36d2002-07-08 16:40:03 +000050
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010051#define OLDPATH "oldpath"
52#define NEWPATH "newpath"
robbiewd62a36d2002-07-08 16:40:03 +000053
subrata_modak56207ce2009-03-23 13:35:39 +000054int main(int ac, char **av)
robbiewd62a36d2002-07-08 16:40:03 +000055{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020056 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020057 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000058
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010059 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000060 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080061
subrata_modak56207ce2009-03-23 13:35:39 +000062 setup();
robbiewd62a36d2002-07-08 16:40:03 +000063
subrata_modak56207ce2009-03-23 13:35:39 +000064 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080065 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000066
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010067 TEST(link(OLDPATH, NEWPATH));
subrata_modakbdbaec52009-02-26 12:14:51 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069 if (TEST_RETURN != -1) {
subrata_modak358c3ee2009-10-26 14:55:46 +000070 tst_resm(TFAIL, "link() returned %ld,"
subrata_modak56207ce2009-03-23 13:35:39 +000071 "expected -1, errno=%d", TEST_RETURN,
Cyril Hrubis605fa332015-02-04 13:11:20 +010072 EACCES);
subrata_modak56207ce2009-03-23 13:35:39 +000073 } else {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010074 if (TEST_ERRNO == EACCES) {
subrata_modak56207ce2009-03-23 13:35:39 +000075 tst_resm(TPASS, "link() fails with expected "
76 "error EACCES errno:%d", TEST_ERRNO);
77 } else {
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010078 tst_resm(TFAIL, "link() fails with "
subrata_modak56207ce2009-03-23 13:35:39 +000079 "errno=%d, expected errno=%d",
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010080 TEST_ERRNO, EACCES);
subrata_modak56207ce2009-03-23 13:35:39 +000081 }
robbiewd62a36d2002-07-08 16:40:03 +000082 }
Garrett Cooper2c282152010-12-16 00:55:50 -080083 }
robbiewd62a36d2002-07-08 16:40:03 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080086 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080087}
robbiewd62a36d2002-07-08 16:40:03 +000088
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010089static void setup(void)
robbiewd62a36d2002-07-08 16:40:03 +000090{
Garrett Cooper91ec53d2010-03-07 14:19:11 -080091 struct passwd *nobody_pwd;
robbiewd62a36d2002-07-08 16:40:03 +000092
subrata_modak56207ce2009-03-23 13:35:39 +000093 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiewd62a36d2002-07-08 16:40:03 +000094
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +010095 tst_require_root(NULL);
96
subrata_modak56207ce2009-03-23 13:35:39 +000097 TEST_PAUSE;
robbiewd62a36d2002-07-08 16:40:03 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 tst_tmpdir();
robbiewd62a36d2002-07-08 16:40:03 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 /* Modify mode permissions on test directory */
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100102 SAFE_CHMOD(cleanup, ".", MODE_TO);
subrata_modak56207ce2009-03-23 13:35:39 +0000103
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100104 SAFE_TOUCH(cleanup, OLDPATH, 0777, NULL);
105 nobody_pwd = SAFE_GETPWNAM(cleanup, "nobody");
106 SAFE_SETEUID(cleanup, nobody_pwd->pw_uid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107}
robbiewd62a36d2002-07-08 16:40:03 +0000108
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100109static void cleanup(void)
robbiewd62a36d2002-07-08 16:40:03 +0000110{
Cyril Hrubisaf0ea7b2014-03-03 19:53:46 +0100111 if (seteuid(0))
112 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
robbiewd62a36d2002-07-08 16:40:03 +0000113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700115}