xref: /openbsd-src/regress/lib/libc/sys/t_umask.c (revision 49a6e16f2c2c8e509184b1f777366d1a6f337e1c)
1 /*	$OpenBSD: t_umask.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $	*/
2 /* $NetBSD: t_umask.c,v 1.2 2017/01/13 19:34:19 christos Exp $ */
3 
4 /*-
5  * Copyright (c) 2011 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jukka Ruohonen.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "macros.h"
34 
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 
38 #include "atf-c.h"
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 static const char path[] = "umask";
45 static const mode_t mask[] = {
46 	S_IRWXU,
47 	S_IRUSR,
48 	S_IWUSR,
49 	S_IXUSR,
50 	S_IRWXG,
51 	S_IRGRP,
52 	S_IWGRP,
53 	S_IXGRP,
54 	S_IRWXO,
55 	S_IROTH,
56 	S_IWOTH,
57 	S_IXOTH
58 };
59 
60 ATF_TC_WITH_CLEANUP(umask_fork);
ATF_TC_HEAD(umask_fork,tc)61 ATF_TC_HEAD(umask_fork, tc)
62 {
63 	atf_tc_set_md_var(tc, "descr", "Check that umask(2) is inherited");
64 }
65 
ATF_TC_BODY(umask_fork,tc)66 ATF_TC_BODY(umask_fork, tc)
67 {
68 	mode_t mode;
69 	pid_t pid;
70 	size_t i;
71 	int sta;
72 
73 	for (i = 0; i < __arraycount(mask) - 1; i++) {
74 
75 		(void)umask(mask[i] | mask[i + 1]);
76 
77 		pid = fork();
78 
79 		if (pid < 0)
80 			continue;
81 
82 		if (pid == 0) {
83 
84 			mode = umask(mask[i]);
85 
86 			if (mode != (mask[i] | mask[i + 1]))
87 				_exit(EXIT_FAILURE);
88 
89 			_exit(EXIT_SUCCESS);
90 		}
91 
92 		(void)wait(&sta);
93 
94 		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
95 			goto fail;
96 	}
97 
98 	return;
99 
100 fail:
101 	(void)umask(S_IWGRP | S_IWOTH);
102 
103 	atf_tc_fail("umask(2) was not inherited");
104 }
105 
ATF_TC_CLEANUP(umask_fork,tc)106 ATF_TC_CLEANUP(umask_fork, tc)
107 {
108 	(void)umask(S_IWGRP | S_IWOTH);
109 }
110 
111 ATF_TC_WITH_CLEANUP(umask_open);
ATF_TC_HEAD(umask_open,tc)112 ATF_TC_HEAD(umask_open, tc)
113 {
114 	atf_tc_set_md_var(tc, "descr", "A basic test of open(2) and umask(2)");
115 }
116 
ATF_TC_BODY(umask_open,tc)117 ATF_TC_BODY(umask_open, tc)
118 {
119 	const char *str = NULL;
120 	struct stat st;
121 	size_t i;
122 	int fd;
123 
124 	for (i = 0; i < __arraycount(mask); i++) {
125 
126 		(void)umask(mask[i]);
127 
128 		fd = open(path, O_RDWR | O_CREAT, 0777);
129 
130 		if (fd < 0)
131 			continue;
132 
133 		(void)close(fd);
134 		(void)memset(&st, 0, sizeof(struct stat));
135 
136 		if (stat(path, &st) != 0) {
137 			str = "failed to stat(2)";
138 			goto out;
139 		}
140 
141 		if ((st.st_mode & mask[i]) != 0) {
142 			str = "invalid umask(2)";
143 			goto out;
144 		}
145 
146 		if (unlink(path) != 0) {
147 			str = "failed to unlink(2)";
148 			goto out;
149 		}
150 
151 	}
152 
153 out:
154 	(void)umask(S_IWGRP | S_IWOTH);
155 
156 	if (str != NULL)
157 		atf_tc_fail("%s", str);
158 }
159 
ATF_TC_CLEANUP(umask_open,tc)160 ATF_TC_CLEANUP(umask_open, tc)
161 {
162 	(void)umask(S_IWGRP | S_IWOTH);
163 	(void)unlink(path);
164 }
165 
166 ATF_TC_WITH_CLEANUP(umask_previous);
ATF_TC_HEAD(umask_previous,tc)167 ATF_TC_HEAD(umask_previous, tc)
168 {
169 	atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)");
170 }
171 
ATF_TC_BODY(umask_previous,tc)172 ATF_TC_BODY(umask_previous, tc)
173 {
174 	mode_t mode;
175 	size_t i;
176 
177 	for (i = 0; i < __arraycount(mask); i++) {
178 
179 		mode = umask(mask[i]);
180 		mode = umask(mask[i]);
181 
182 		if (mode != mask[i])
183 			goto fail;
184 	}
185 
186 	return;
187 
188 fail:
189 	(void)umask(S_IWGRP | S_IWOTH);
190 
191 	atf_tc_fail("umask(2) did not return the previous mask");
192 }
193 
ATF_TC_CLEANUP(umask_previous,tc)194 ATF_TC_CLEANUP(umask_previous, tc)
195 {
196 	(void)umask(S_IWGRP | S_IWOTH);
197 }
198 
ATF_TP_ADD_TCS(tp)199 ATF_TP_ADD_TCS(tp)
200 {
201 
202 	ATF_TP_ADD_TC(tp, umask_fork);
203 	ATF_TP_ADD_TC(tp, umask_open);
204 	ATF_TP_ADD_TC(tp, umask_previous);
205 
206 	return atf_no_error();
207 }
208