Deleted Added
full compact
t_fchownat.c (276478) t_fchownat.c (277443)
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>
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
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
53static int getuser(uid_t *, gid_t *);
54
55static 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
68ATF_TC(fchownat_fd);
69ATF_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}
74ATF_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
96ATF_TC(fchownat_fdcwd);
97ATF_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}
103ATF_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
123ATF_TC(fchownat_fdcwderr);
124ATF_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}
130ATF_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
140ATF_TC(fchownat_fderr1);
141ATF_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}
146ATF_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
159ATF_TC(fchownat_fderr2);
160ATF_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}
165ATF_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
183ATF_TC(fchownat_fderr3);
184ATF_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}
189ATF_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
203ATF_TC(fchownat_fdlink);
204ATF_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}
209ATF_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
235ATF_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}
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}