1 /* $NetBSD: t_fchownat.c,v 1.4 2017/01/10 15:13:56 christos 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.4 2017/01/10 15:13:56 christos Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/stat.h> 36 #include <atf-c.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <limits.h> 40 #include <paths.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <pwd.h> 45 46 #define DIR "dir" 47 #define FILE "dir/fchownat" 48 #define BASEFILE "fchownat" 49 #define LINK "dir/symlink" 50 #define BASELINK "symlink" 51 #define FILEERR "dir/fchownaterr" 52 #define USER "nobody" 53 54 static int getuser(uid_t *, gid_t *); 55 56 static int getuser(uid_t *uid, gid_t *gid) 57 { 58 struct passwd *pw; 59 60 if ((pw = getpwnam(USER)) == NULL) 61 return -1; 62 63 *uid = pw->pw_uid; 64 *gid = pw->pw_gid; 65 66 return 0; 67 } 68 69 ATF_TC(fchownat_fd); 70 ATF_TC_HEAD(fchownat_fd, tc) 71 { 72 atf_tc_set_md_var(tc, "descr", "See that fchownat works with fd"); 73 atf_tc_set_md_var(tc, "require.user", "root"); 74 } 75 ATF_TC_BODY(fchownat_fd, tc) 76 { 77 int dfd; 78 int fd; 79 uid_t uid; 80 gid_t gid; 81 struct stat st; 82 83 ATF_REQUIRE(getuser(&uid, &gid) == 0); 84 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 85 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 86 ATF_REQUIRE(close(fd) == 0); 87 88 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 89 ATF_REQUIRE(fchownat(dfd, BASEFILE, uid, gid, 0) == 0); 90 ATF_REQUIRE(close(dfd) == 0); 91 92 ATF_REQUIRE(stat(FILE, &st) == 0); 93 ATF_REQUIRE(st.st_uid == uid); 94 ATF_REQUIRE(st.st_gid == gid); 95 } 96 97 ATF_TC(fchownat_fdcwd); 98 ATF_TC_HEAD(fchownat_fdcwd, tc) 99 { 100 atf_tc_set_md_var(tc, "descr", 101 "See that fchownat works with fd as AT_FDCWD"); 102 atf_tc_set_md_var(tc, "require.user", "root"); 103 } 104 ATF_TC_BODY(fchownat_fdcwd, tc) 105 { 106 int fd; 107 uid_t uid; 108 gid_t gid; 109 struct stat st; 110 111 ATF_REQUIRE(getuser(&uid, &gid) == 0); 112 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 113 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 114 ATF_REQUIRE(close(fd) == 0); 115 116 ATF_REQUIRE(chdir(DIR) == 0); 117 ATF_REQUIRE(fchownat(AT_FDCWD, BASEFILE, uid, gid, 0) == 0); 118 119 ATF_REQUIRE(stat(BASEFILE, &st) == 0); 120 ATF_REQUIRE(st.st_uid == uid); 121 ATF_REQUIRE(st.st_gid == gid); 122 } 123 124 ATF_TC(fchownat_fdcwderr); 125 ATF_TC_HEAD(fchownat_fdcwderr, tc) 126 { 127 atf_tc_set_md_var(tc, "descr", 128 "See that fchownat fails with fd as AT_FDCWD and bad path"); 129 atf_tc_set_md_var(tc, "require.user", "root"); 130 } 131 ATF_TC_BODY(fchownat_fdcwderr, tc) 132 { 133 uid_t uid; 134 gid_t gid; 135 136 ATF_REQUIRE(getuser(&uid, &gid) == 0); 137 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 138 ATF_REQUIRE(fchownat(AT_FDCWD, FILEERR, uid, gid, 0) == -1); 139 } 140 141 ATF_TC(fchownat_fderr1); 142 ATF_TC_HEAD(fchownat_fderr1, tc) 143 { 144 atf_tc_set_md_var(tc, "descr", "See that fchownat fail with bad path"); 145 atf_tc_set_md_var(tc, "require.user", "root"); 146 } 147 ATF_TC_BODY(fchownat_fderr1, tc) 148 { 149 int dfd; 150 uid_t uid; 151 gid_t gid; 152 153 ATF_REQUIRE(getuser(&uid, &gid) == 0); 154 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 155 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 156 ATF_REQUIRE(fchownat(dfd, FILEERR, uid, gid, 0) == -1); 157 ATF_REQUIRE(close(dfd) == 0); 158 } 159 160 ATF_TC(fchownat_fderr2); 161 ATF_TC_HEAD(fchownat_fderr2, tc) 162 { 163 atf_tc_set_md_var(tc, "descr", "See that fchownat fails with bad fdat"); 164 atf_tc_set_md_var(tc, "require.user", "root"); 165 } 166 ATF_TC_BODY(fchownat_fderr2, tc) 167 { 168 int dfd; 169 int fd; 170 char cwd[MAXPATHLEN]; 171 uid_t uid; 172 gid_t gid; 173 174 ATF_REQUIRE(getuser(&uid, &gid) == 0); 175 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 176 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 177 ATF_REQUIRE(close(fd) == 0); 178 179 ATF_REQUIRE((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)) != -1); 180 ATF_REQUIRE(fchownat(dfd, BASEFILE, uid, gid, 0) == -1); 181 ATF_REQUIRE(close(dfd) == 0); 182 } 183 184 ATF_TC(fchownat_fderr3); 185 ATF_TC_HEAD(fchownat_fderr3, tc) 186 { 187 atf_tc_set_md_var(tc, "descr", "See that fchownat fails with fd as -1"); 188 atf_tc_set_md_var(tc, "require.user", "root"); 189 } 190 ATF_TC_BODY(fchownat_fderr3, tc) 191 { 192 int fd; 193 uid_t uid; 194 gid_t gid; 195 196 ATF_REQUIRE(getuser(&uid, &gid) == 0); 197 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 198 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 199 ATF_REQUIRE(close(fd) == 0); 200 201 ATF_REQUIRE(fchownat(-1, FILE, uid, gid, 0) == -1); 202 } 203 204 ATF_TC(fchownat_fdlink); 205 ATF_TC_HEAD(fchownat_fdlink, tc) 206 { 207 atf_tc_set_md_var(tc, "descr", "See that fchownat works on symlink"); 208 atf_tc_set_md_var(tc, "require.user", "root"); 209 } 210 ATF_TC_BODY(fchownat_fdlink, tc) 211 { 212 int dfd; 213 uid_t uid; 214 gid_t gid; 215 struct stat st; 216 217 ATF_REQUIRE(getuser(&uid, &gid) == 0); 218 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 219 ATF_REQUIRE(symlink(FILE, LINK) == 0); /* Target does not exists */ 220 221 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 222 223 ATF_REQUIRE(fchownat(dfd, BASELINK, uid, gid, 0) == -1); 224 ATF_REQUIRE(errno == ENOENT); 225 226 ATF_REQUIRE(fchownat(dfd, BASELINK, uid, gid, 227 AT_SYMLINK_NOFOLLOW) == 0); 228 229 ATF_REQUIRE(close(dfd) == 0); 230 231 ATF_REQUIRE(lstat(LINK, &st) == 0); 232 ATF_REQUIRE(st.st_uid == uid); 233 ATF_REQUIRE(st.st_gid == gid); 234 } 235 236 ATF_TP_ADD_TCS(tp) 237 { 238 239 ATF_TP_ADD_TC(tp, fchownat_fd); 240 ATF_TP_ADD_TC(tp, fchownat_fdcwd); 241 ATF_TP_ADD_TC(tp, fchownat_fdcwderr); 242 ATF_TP_ADD_TC(tp, fchownat_fderr1); 243 ATF_TP_ADD_TC(tp, fchownat_fderr2); 244 ATF_TP_ADD_TC(tp, fchownat_fderr3); 245 ATF_TP_ADD_TC(tp, fchownat_fdlink); 246 247 return atf_no_error(); 248 } 249