xref: /freebsd/sys/security/audit/bsm_fcntl.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*-
2  * Copyright (c) 2008-2009 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 
36 #include <security/audit/audit.h>
37 
38 #include <bsm/audit_fcntl.h>
39 #include <bsm/audit_record.h>
40 
41 struct bsm_fcntl_cmd {
42 	u_short	bfc_bsm_fcntl_cmd;
43 	int	bfc_local_fcntl_cmd;
44 };
45 typedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
46 
47 static const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
48 	{ BSM_F_DUPFD, 		F_DUPFD },
49 	{ BSM_F_GETFD,		F_GETFD },
50 	{ BSM_F_SETFD,		F_SETFD	},
51 	{ BSM_F_GETFL,		F_GETFL },
52 	{ BSM_F_SETFL,		F_SETFL },
53 #ifdef	F_O_GETLK
54 	{ BSM_F_O_GETLK,	F_O_GETLK },
55 #endif
56 	{ BSM_F_SETLK,		F_SETLK },
57 	{ BSM_F_SETLKW,		F_SETLK },
58 #ifdef	F_CHFL
59 	{ BSM_F_CHKFL,		F_CHKFL },
60 #endif
61 #ifdef 	F_DUP2FD
62 	{ BSM_F_DUP2FD,		F_DUP2FD },
63 #endif
64 #ifdef	F_ALLOCSP
65 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
66 #endif
67 #ifdef	F_FREESP
68 	{ BSM_F_FREESP,		F_FREESP },
69 #endif
70 #ifdef	F_ISSTREAM
71 	{ BSM_F_ISSTREAM,	F_ISSTREAM},
72 #endif
73 	{ BSM_F_GETLK,		F_GETLK },
74 #ifdef 	F_PRIV
75 	{ BSM_F_PRIV,		F_PRIV },
76 #endif
77 #ifdef	F_NPRIV
78 	{ BSM_F_NPRIV,		F_NPRIV },
79 #endif
80 #ifdef 	F_QUOTACTL
81 	{ BSM_F_QUOTACTL,	F_QUOTACTL },
82 #endif
83 #ifdef	F_BLOCKS
84 	{ BSM_F_BLOCKS,		F_BLOCKS },
85 #endif
86 #ifdef	F_BLKSIZE
87 	{ BSM_F_BLKSIZE,	F_BLKSIZE },
88 #endif
89 	{ BSM_F_GETOWN,		F_GETOWN },
90 	{ BSM_F_SETOWN,		F_SETOWN },
91 #ifdef	F_REVOKE
92 	{ BSM_F_REVOKE,		F_REVOKE },
93 #endif
94 #ifdef 	F_HASREMOTEBLOCKS
95 	{ BSM_F_HASREMOTEBLOCKS,
96 				F_HASREMOTEBLOCKS },
97 #endif
98 #ifdef 	F_FREESP
99 	{ BSM_F_FREESP,		F_FREESP },
100 #endif
101 #ifdef 	F_ALLOCSP
102 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
103 #endif
104 #ifdef	F_FREESP64
105 	{ BSM_F_FREESP64,	F_FREESP64 },
106 #endif
107 #ifdef 	F_ALLOCSP64
108 	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
109 #endif
110 #ifdef	F_GETLK64
111 	{ BSM_F_GETLK64, 	F_GETLK64 },
112 #endif
113 #ifdef	F_SETLK64
114 	{ BSM_F_SETLK64, 	F_SETLK64 },
115 #endif
116 #ifdef	F_SETLKW64
117 	{ BSM_F_SETLKW64, 	F_SETLKW64 },
118 #endif
119 #ifdef	F_SHARE
120 	{ BSM_F_SHARE,		F_SHARE },
121 #endif
122 #ifdef	F_UNSHARE
123 	{ BSM_F_UNSHARE,	F_UNSHARE },
124 #endif
125 #ifdef	F_SETLK_NBMAND
126 	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
127 #endif
128 #ifdef	F_SHARE_NBMAND
129 	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
130 #endif
131 #ifdef	F_SETLK64_NBMAND
132 	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
133 #endif
134 #ifdef	F_GETXFL
135 	{ BSM_F_GETXFL,		F_GETXFL },
136 #endif
137 #ifdef	F_BADFD
138 	{ BSM_F_BADFD,		F_BADFD },
139 #endif
140 #ifdef	F_OGETLK
141 	{ BSM_F_OGETLK,		F_OGETLK },
142 #endif
143 #ifdef	F_OSETLK
144 	{ BSM_F_OSETLK,		F_OSETLK },
145 #endif
146 #ifdef	F_OSETLKW
147 	{ BSM_F_OSETLKW,	F_OSETLKW },
148 #endif
149 #ifdef	F_SETLK_REMOTE
150 	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
151 #endif
152 
153 #ifdef	F_SETSIG
154 	{ BSM_F_SETSIG,		F_SETSIG },
155 #endif
156 #ifdef	F_GETSIG
157 	{ BSM_F_GETSIG,		F_GETSIG },
158 #endif
159 
160 #ifdef	F_CHKCLEAN
161 	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
162 #endif
163 #ifdef	F_PREALLOCATE
164 	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
165 #endif
166 #ifdef	F_SETSIZE
167 	{ BSM_F_SETSIZE,	F_SETSIZE },
168 #endif
169 #ifdef	F_RDADVISE
170 	{ BSM_F_RDADVISE,	F_RDADVISE },
171 #endif
172 #ifdef	F_RDAHEAD
173 	{ BSM_F_RDAHEAD,	F_RDAHEAD },
174 #endif
175 #ifdef	F_READBOOTSTRAP
176 	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
177 #endif
178 #ifdef	F_WRITEBOOTSTRAP
179 	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
180 #endif
181 #ifdef	F_NOCACHE
182 	{ BSM_F_NOCACHE,	F_NOCACHE },
183 #endif
184 #ifdef	F_LOG2PHYS
185 	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
186 #endif
187 #ifdef	F_GETPATH
188 	{ BSM_F_GETPATH,	F_GETPATH },
189 #endif
190 #ifdef	F_FULLFSYNC
191 	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
192 #endif
193 #ifdef	F_PATHPKG_CHECK
194 	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
195 #endif
196 #ifdef	F_FREEZE_FS
197 	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
198 #endif
199 #ifdef	F_THAW_FS
200 	{ BSM_F_THAW_FS,	F_THAW_FS },
201 #endif
202 #ifdef	F_GLOBAL_NOCACHE
203 	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
204 #endif
205 #ifdef	F_OPENFROM
206 	{ BSM_F_OPENFROM,	F_OPENFROM },
207 #endif
208 #ifdef	F_UNLINKFROM
209 	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
210 #endif
211 #ifdef	F_CHECK_OPENEVT
212 	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
213 #endif
214 #ifdef	F_ADDSIGS
215 	{ BSM_F_ADDSIGS,	F_ADDSIGS },
216 #endif
217 #ifdef	F_MARKDEPENDENCY
218 	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
219 #endif
220 
221 #ifdef	FCNTL_FS_SPECIFIC_BASE
222 	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
223 	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
224 	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
225 	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
226 	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
227 	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
228 	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
229 	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
230 	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
231 	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
232 	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
233 	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
234 	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
235 	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
236 	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
237 	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
238 #endif	/* FCNTL_FS_SPECIFIC_BASE */
239 };
240 static const int bsm_fcntl_cmd_count = nitems(bsm_fcntl_cmdtab);
241 
242 static const bsm_fcntl_cmd_t *
243 bsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
244 {
245 	int i;
246 
247 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
248 		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
249 		    local_fcntl_cmd)
250 			return (&bsm_fcntl_cmdtab[i]);
251 	}
252 	return (NULL);
253 }
254 
255 u_short
256 au_fcntl_cmd_to_bsm(int local_fcntl_cmd)
257 {
258 	const bsm_fcntl_cmd_t *bfcp;
259 
260 	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
261 	if (bfcp == NULL)
262 		return (BSM_F_UNKNOWN);
263 	return (bfcp->bfc_bsm_fcntl_cmd);
264 }
265 
266 static const bsm_fcntl_cmd_t *
267 bsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
268 {
269 	int i;
270 
271 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
272 		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
273 		    bsm_fcntl_cmd)
274 			return (&bsm_fcntl_cmdtab[i]);
275 	}
276 	return (NULL);
277 }
278 
279 int
280 au_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
281 {
282 	const bsm_fcntl_cmd_t *bfcp;
283 
284 	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
285 	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
286 		return (-1);
287 	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
288 	return (0);
289 }
290