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