xref: /freebsd/sys/security/audit/bsm_fcntl.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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  * P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_fcntl.c#2
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/fcntl.h>
37 
38 #include <security/audit/audit.h>
39 
40 #include <bsm/audit_fcntl.h>
41 #include <bsm/audit_record.h>
42 
43 struct bsm_fcntl_cmd {
44 	u_short	bfc_bsm_fcntl_cmd;
45 	int	bfc_local_fcntl_cmd;
46 };
47 typedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
48 
49 static const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
50 	{ BSM_F_DUPFD, 		F_DUPFD },
51 	{ BSM_F_GETFD,		F_GETFD },
52 	{ BSM_F_SETFD,		F_SETFD	},
53 	{ BSM_F_GETFL,		F_GETFL },
54 	{ BSM_F_SETFL,		F_SETFL },
55 #ifdef	F_O_GETLK
56 	{ BSM_F_O_GETLK,	F_O_GETLK },
57 #endif
58 	{ BSM_F_SETLK,		F_SETLK },
59 	{ BSM_F_SETLKW,		F_SETLK },
60 #ifdef	F_CHFL
61 	{ BSM_F_CHKFL,		F_CHKFL },
62 #endif
63 #ifdef 	F_DUP2FD
64 	{ BSM_F_DUP2FD,		F_DUP2FD },
65 #endif
66 #ifdef	F_ALLOCSP
67 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
68 #endif
69 #ifdef	F_FREESP
70 	{ BSM_F_FREESP,		F_FREESP },
71 #endif
72 #ifdef	F_ISSTREAM
73 	{ BSM_F_ISSTREAM,	F_ISSTREAM},
74 #endif
75 	{ BSM_F_GETLK,		F_GETLK },
76 #ifdef 	F_PRIV
77 	{ BSM_F_PRIV,		F_PRIV },
78 #endif
79 #ifdef	F_NPRIV
80 	{ BSM_F_NPRIV,		F_NPRIV },
81 #endif
82 #ifdef 	F_QUOTACTL
83 	{ BSM_F_QUOTACTL,	F_QUOTACTL },
84 #endif
85 #ifdef	F_BLOCKS
86 	{ BSM_F_BLOCKS,		F_BLOCKS },
87 #endif
88 #ifdef	F_BLKSIZE
89 	{ BSM_F_BLKSIZE,	F_BLKSIZE },
90 #endif
91 	{ BSM_F_GETOWN,		F_GETOWN },
92 	{ BSM_F_SETOWN,		F_SETOWN },
93 #ifdef	F_REVOKE
94 	{ BSM_F_REVOKE,		F_REVOKE },
95 #endif
96 #ifdef 	F_HASREMOTEBLOCKS
97 	{ BSM_F_HASREMOTEBLOCKS,
98 				F_HASREMOTEBLOCKS },
99 #endif
100 #ifdef 	F_FREESP
101 	{ BSM_F_FREESP,		F_FREESP },
102 #endif
103 #ifdef 	F_ALLOCSP
104 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
105 #endif
106 #ifdef	F_FREESP64
107 	{ BSM_F_FREESP64,	F_FREESP64 },
108 #endif
109 #ifdef 	F_ALLOCSP64
110 	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
111 #endif
112 #ifdef	F_GETLK64
113 	{ BSM_F_GETLK64, 	F_GETLK64 },
114 #endif
115 #ifdef	F_SETLK64
116 	{ BSM_F_SETLK64, 	F_SETLK64 },
117 #endif
118 #ifdef	F_SETLKW64
119 	{ BSM_F_SETLKW64, 	F_SETLKW64 },
120 #endif
121 #ifdef	F_SHARE
122 	{ BSM_F_SHARE,		F_SHARE },
123 #endif
124 #ifdef	F_UNSHARE
125 	{ BSM_F_UNSHARE,	F_UNSHARE },
126 #endif
127 #ifdef	F_SETLK_NBMAND
128 	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
129 #endif
130 #ifdef	F_SHARE_NBMAND
131 	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
132 #endif
133 #ifdef	F_SETLK64_NBMAND
134 	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
135 #endif
136 #ifdef	F_GETXFL
137 	{ BSM_F_GETXFL,		F_GETXFL },
138 #endif
139 #ifdef	F_BADFD
140 	{ BSM_F_BADFD,		F_BADFD },
141 #endif
142 #ifdef	F_OGETLK
143 	{ BSM_F_OGETLK,		F_OGETLK },
144 #endif
145 #ifdef	F_OSETLK
146 	{ BSM_F_OSETLK,		F_OSETLK },
147 #endif
148 #ifdef	F_OSETLKW
149 	{ BSM_F_OSETLKW,	F_OSETLKW },
150 #endif
151 #ifdef	F_SETLK_REMOTE
152 	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
153 #endif
154 
155 #ifdef	F_SETSIG
156 	{ BSM_F_SETSIG,		F_SETSIG },
157 #endif
158 #ifdef	F_GETSIG
159 	{ BSM_F_GETSIG,		F_GETSIG },
160 #endif
161 
162 #ifdef	F_CHKCLEAN
163 	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
164 #endif
165 #ifdef	F_PREALLOCATE
166 	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
167 #endif
168 #ifdef	F_SETSIZE
169 	{ BSM_F_SETSIZE,	F_SETSIZE },
170 #endif
171 #ifdef	F_RDADVISE
172 	{ BSM_F_RDADVISE,	F_RDADVISE },
173 #endif
174 #ifdef	F_RDAHEAD
175 	{ BSM_F_RDAHEAD,	F_RDAHEAD },
176 #endif
177 #ifdef	F_READBOOTSTRAP
178 	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
179 #endif
180 #ifdef	F_WRITEBOOTSTRAP
181 	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
182 #endif
183 #ifdef	F_NOCACHE
184 	{ BSM_F_NOCACHE,	F_NOCACHE },
185 #endif
186 #ifdef	F_LOG2PHYS
187 	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
188 #endif
189 #ifdef	F_GETPATH
190 	{ BSM_F_GETPATH,	F_GETPATH },
191 #endif
192 #ifdef	F_FULLFSYNC
193 	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
194 #endif
195 #ifdef	F_PATHPKG_CHECK
196 	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
197 #endif
198 #ifdef	F_FREEZE_FS
199 	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
200 #endif
201 #ifdef	F_THAW_FS
202 	{ BSM_F_THAW_FS,	F_THAW_FS },
203 #endif
204 #ifdef	F_GLOBAL_NOCACHE
205 	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
206 #endif
207 #ifdef	F_OPENFROM
208 	{ BSM_F_OPENFROM,	F_OPENFROM },
209 #endif
210 #ifdef	F_UNLINKFROM
211 	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
212 #endif
213 #ifdef	F_CHECK_OPENEVT
214 	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
215 #endif
216 #ifdef	F_ADDSIGS
217 	{ BSM_F_ADDSIGS,	F_ADDSIGS },
218 #endif
219 #ifdef	F_MARKDEPENDENCY
220 	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
221 #endif
222 
223 #ifdef	FCNTL_FS_SPECIFIC_BASE
224 	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
225 	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
226 	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
227 	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
228 	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
229 	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
230 	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
231 	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
232 	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
233 	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
234 	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
235 	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
236 	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
237 	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
238 	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
239 	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
240 #endif	/* FCNTL_FS_SPECIFIC_BASE */
241 };
242 static const int bsm_fcntl_cmd_count = sizeof(bsm_fcntl_cmdtab) /
243 	    sizeof(bsm_fcntl_cmdtab[0]);
244 
245 static const bsm_fcntl_cmd_t *
246 bsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
247 {
248 	int i;
249 
250 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
251 		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
252 		    local_fcntl_cmd)
253 			return (&bsm_fcntl_cmdtab[i]);
254 	}
255 	return (NULL);
256 }
257 
258 u_short
259 au_fcntl_cmd_to_bsm(int local_fcntl_cmd)
260 {
261 	const bsm_fcntl_cmd_t *bfcp;
262 
263 	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
264 	if (bfcp == NULL)
265 		return (BSM_F_UNKNOWN);
266 	return (bfcp->bfc_bsm_fcntl_cmd);
267 }
268 
269 static const bsm_fcntl_cmd_t *
270 bsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
271 {
272 	int i;
273 
274 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
275 		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
276 		    bsm_fcntl_cmd)
277 			return (&bsm_fcntl_cmdtab[i]);
278 	}
279 	return (NULL);
280 }
281 
282 int
283 au_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
284 {
285 	const bsm_fcntl_cmd_t *bfcp;
286 
287 	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
288 	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
289 		return (-1);
290 	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
291 	return (0);
292 }
293