robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 1 | /* |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 2 | * Copyright (C) Bull S.A. 2001 |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 4 | * 06/2002 Ported by Jacky Malcles |
| 5 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 7 | * 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. |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 12 | * 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. |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 17 | * 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 |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 23 | * 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. |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 26 | */ |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 27 | |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 28 | #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 Cooper | 91ec53d | 2010-03-07 14:19:11 -0800 | [diff] [blame] | 37 | #include <pwd.h> |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 38 | |
| 39 | #include "test.h" |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 40 | #include "safe_macros.h" |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 41 | |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 42 | #define NOBODY_USER 99 |
| 43 | #define MODE_TO S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH |
| 44 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 45 | static void setup(void); |
| 46 | static void cleanup(void); |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 47 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 48 | char *TCID = "link06"; |
| 49 | int TST_TOTAL = 1; |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 50 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 51 | #define OLDPATH "oldpath" |
| 52 | #define NEWPATH "newpath" |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 53 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | int main(int ac, char **av) |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 55 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 56 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 57 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 59 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 60 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 61 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | setup(); |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 63 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 65 | tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 66 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 67 | TEST(link(OLDPATH, NEWPATH)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | if (TEST_RETURN != -1) { |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 70 | tst_resm(TFAIL, "link() returned %ld," |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | "expected -1, errno=%d", TEST_RETURN, |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 72 | EACCES); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | } else { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 74 | if (TEST_ERRNO == EACCES) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | tst_resm(TPASS, "link() fails with expected " |
| 76 | "error EACCES errno:%d", TEST_ERRNO); |
| 77 | } else { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 78 | tst_resm(TFAIL, "link() fails with " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | "errno=%d, expected errno=%d", |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 80 | TEST_ERRNO, EACCES); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | } |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 82 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 83 | } |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 86 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 87 | } |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 88 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 89 | static void setup(void) |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 90 | { |
Garrett Cooper | 91ec53d | 2010-03-07 14:19:11 -0800 | [diff] [blame] | 91 | struct passwd *nobody_pwd; |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 94 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 95 | tst_require_root(NULL); |
| 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | TEST_PAUSE; |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | tst_tmpdir(); |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | /* Modify mode permissions on test directory */ |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 102 | SAFE_CHMOD(cleanup, ".", MODE_TO); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 104 | SAFE_TOUCH(cleanup, OLDPATH, 0777, NULL); |
| 105 | nobody_pwd = SAFE_GETPWNAM(cleanup, "nobody"); |
| 106 | SAFE_SETEUID(cleanup, nobody_pwd->pw_uid); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | } |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 108 | |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 109 | static void cleanup(void) |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 110 | { |
Cyril Hrubis | af0ea7b | 2014-03-03 19:53:46 +0100 | [diff] [blame] | 111 | if (seteuid(0)) |
| 112 | tst_resm(TWARN | TERRNO, "seteuid(0) failed"); |
robbiew | d62a36d | 2002-07-08 16:40:03 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 115 | } |