1 /* $NetBSD: t_fchownat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_fchownat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $"); 33 34 #include <atf-c.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <limits.h> 38 #include <paths.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <pwd.h> 43 #include <sys/param.h> 44 45 #define DIR "dir" 46 #define FILE "dir/fchownat" 47 #define BASEFILE "fchownat" 48 #define LINK "dir/symlink" 49 #define BASELINK "symlink" 50 #define FILEERR "dir/fchownaterr" 51 #define USER "nobody" 52 53 static int getuser(uid_t *, gid_t *); 54 55 static int getuser(uid_t *uid, gid_t *gid) 56 { 57 struct passwd *pw; 58 59 if ((pw = getpwnam(USER)) == NULL) 60 return -1; 61 62 *uid = pw->pw_uid; 63 *gid = pw->pw_gid; 64 65 return 0; 66 } 67 68 ATF_TC(fchownat_fd); 69 ATF_TC_HEAD(fchownat_fd, tc) 70 { 71 atf_tc_set_md_var(tc, "descr", "See that fchownat works with fd"); 72 atf_tc_set_md_var(tc, "require.user", "root"); 73 } 74 ATF_TC_BODY(fchownat_fd, tc) 75 { 76 int dfd; 77 int fd; 78 uid_t uid; 79 gid_t gid; 80 struct stat st; 81 82 ATF_REQUIRE(getuser(&uid, &gid) == 0); 83 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 84 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 85 ATF_REQUIRE(close(fd) == 0); 86 87 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 88 ATF_REQUIRE(fchownat(dfd, BASEFILE, uid, gid, 0) == 0); 89 ATF_REQUIRE(close(dfd) == 0); 90 91 ATF_REQUIRE(stat(FILE, &st) == 0); 92 ATF_REQUIRE(st.st_uid == uid); 93 ATF_REQUIRE(st.st_gid == gid); 94 } 95 96 ATF_TC(fchownat_fdcwd); 97 ATF_TC_HEAD(fchownat_fdcwd, tc) 98 { 99 atf_tc_set_md_var(tc, "descr", 100 "See that fchownat works with fd as AT_FDCWD"); 101 atf_tc_set_md_var(tc, "require.user", "root"); 102 } 103 ATF_TC_BODY(fchownat_fdcwd, tc) 104 { 105 int fd; 106 uid_t uid; 107 gid_t gid; 108 struct stat st; 109 110 ATF_REQUIRE(getuser(&uid, &gid) == 0); 111 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 112 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 113 ATF_REQUIRE(close(fd) == 0); 114 115 ATF_REQUIRE(chdir(DIR) == 0); 116 ATF_REQUIRE(fchownat(AT_FDCWD, BASEFILE, uid, gid, 0) == 0); 117 118 ATF_REQUIRE(stat(BASEFILE, &st) == 0); 119 ATF_REQUIRE(st.st_uid == uid); 120 ATF_REQUIRE(st.st_gid == gid); 121 } 122 123 ATF_TC(fchownat_fdcwderr); 124 ATF_TC_HEAD(fchownat_fdcwderr, tc) 125 { 126 atf_tc_set_md_var(tc, "descr", 127 "See that fchownat fails with fd as AT_FDCWD and bad path"); 128 atf_tc_set_md_var(tc, "require.user", "root"); 129 } 130 ATF_TC_BODY(fchownat_fdcwderr, tc) 131 { 132 uid_t uid; 133 gid_t gid; 134 135 ATF_REQUIRE(getuser(&uid, &gid) == 0); 136 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 137 ATF_REQUIRE(fchownat(AT_FDCWD, FILEERR, uid, gid, 0) == -1); 138 } 139 140 ATF_TC(fchownat_fderr1); 141 ATF_TC_HEAD(fchownat_fderr1, tc) 142 { 143 atf_tc_set_md_var(tc, "descr", "See that fchownat fail with bad path"); 144 atf_tc_set_md_var(tc, "require.user", "root"); 145 } 146 ATF_TC_BODY(fchownat_fderr1, tc) 147 { 148 int dfd; 149 uid_t uid; 150 gid_t gid; 151 152 ATF_REQUIRE(getuser(&uid, &gid) == 0); 153 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 154 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 155 ATF_REQUIRE(fchownat(dfd, FILEERR, uid, gid, 0) == -1); 156 ATF_REQUIRE(close(dfd) == 0); 157 } 158 159 ATF_TC(fchownat_fderr2); 160 ATF_TC_HEAD(fchownat_fderr2, tc) 161 { 162 atf_tc_set_md_var(tc, "descr", "See that fchownat fails with bad fdat"); 163 atf_tc_set_md_var(tc, "require.user", "root"); 164 } 165 ATF_TC_BODY(fchownat_fderr2, tc) 166 { 167 int dfd; 168 int fd; 169 char cwd[MAXPATHLEN]; 170 uid_t uid; 171 gid_t gid; 172 173 ATF_REQUIRE(getuser(&uid, &gid) == 0); 174 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 175 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 176 ATF_REQUIRE(close(fd) == 0); 177 178 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 179 ATF_REQUIRE(fchownat(dfd, BASEFILE, uid, gid, 0) == -1); 180 ATF_REQUIRE(close(dfd) == 0); 181 } 182 183 ATF_TC(fchownat_fderr3); 184 ATF_TC_HEAD(fchownat_fderr3, tc) 185 { 186 atf_tc_set_md_var(tc, "descr", "See that fchownat fails with fd as -1"); 187 atf_tc_set_md_var(tc, "require.user", "root"); 188 } 189 ATF_TC_BODY(fchownat_fderr3, tc) 190 { 191 int fd; 192 uid_t uid; 193 gid_t gid; 194 195 ATF_REQUIRE(getuser(&uid, &gid) == 0); 196 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 197 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 198 ATF_REQUIRE(close(fd) == 0); 199 200 ATF_REQUIRE(fchownat(-1, FILE, uid, gid, 0) == -1); 201 } 202 203 ATF_TC(fchownat_fdlink); 204 ATF_TC_HEAD(fchownat_fdlink, tc) 205 { 206 atf_tc_set_md_var(tc, "descr", "See that fchownat works on symlink"); 207 atf_tc_set_md_var(tc, "require.user", "root"); 208 } 209 ATF_TC_BODY(fchownat_fdlink, tc) 210 { 211 int dfd; 212 uid_t uid; 213 gid_t gid; 214 struct stat st; 215 216 ATF_REQUIRE(getuser(&uid, &gid) == 0); 217 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 218 ATF_REQUIRE(symlink(FILE, LINK) == 0); /* Target does not exists */ 219 220 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 221 222 ATF_REQUIRE(fchownat(dfd, BASELINK, uid, gid, 0) == -1); 223 ATF_REQUIRE(errno == ENOENT); 224 225 ATF_REQUIRE(fchownat(dfd, BASELINK, uid, gid, 226 AT_SYMLINK_NOFOLLOW) == 0); 227 228 ATF_REQUIRE(close(dfd) == 0); 229 230 ATF_REQUIRE(lstat(LINK, &st) == 0); 231 ATF_REQUIRE(st.st_uid == uid); 232 ATF_REQUIRE(st.st_gid == gid); 233 } 234 235 ATF_TP_ADD_TCS(tp) 236 { 237 238 ATF_TP_ADD_TC(tp, fchownat_fd); 239 ATF_TP_ADD_TC(tp, fchownat_fdcwd); 240 ATF_TP_ADD_TC(tp, fchownat_fdcwderr); 241 ATF_TP_ADD_TC(tp, fchownat_fderr1); 242 ATF_TP_ADD_TC(tp, fchownat_fderr2); 243 ATF_TP_ADD_TC(tp, fchownat_fderr3); 244 ATF_TP_ADD_TC(tp, fchownat_fdlink); 245 246 return atf_no_error(); 247 } 248