1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93
32 */
33
34 #ifndef _SYS_FILEDESC_H_
35 #define _SYS_FILEDESC_H_
36
37 #include <sys/caprights.h>
38 #include <sys/queue.h>
39 #include <sys/event.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/priority.h>
43 #include <sys/seqc.h>
44 #include <sys/sx.h>
45 #include <sys/_smr.h>
46 #include <sys/smr_types.h>
47
48 #include <machine/_limits.h>
49
50 struct filecaps {
51 cap_rights_t fc_rights; /* per-descriptor capability rights */
52 u_long *fc_ioctls; /* per-descriptor allowed ioctls */
53 int16_t fc_nioctls; /* fc_ioctls array size */
54 uint32_t fc_fcntls; /* per-descriptor allowed fcntls */
55 };
56
57 struct filedescent {
58 struct file *fde_file; /* file structure for open file */
59 struct filecaps fde_caps; /* per-descriptor rights */
60 uint8_t fde_flags; /* per-process open file flags */
61 seqc_t fde_seqc; /* keep file and caps in sync */
62 };
63 #define fde_rights fde_caps.fc_rights
64 #define fde_fcntls fde_caps.fc_fcntls
65 #define fde_ioctls fde_caps.fc_ioctls
66 #define fde_nioctls fde_caps.fc_nioctls
67 #define fde_change_size (offsetof(struct filedescent, fde_seqc))
68
69 struct fdescenttbl {
70 int fdt_nfiles; /* number of open files allocated */
71 struct filedescent fdt_ofiles[0]; /* open files */
72 };
73 #define fd_seqc(fdt, fd) (&(fdt)->fdt_ofiles[(fd)].fde_seqc)
74
75 /*
76 * This structure is used for the management of descriptors. It may be
77 * shared by multiple processes.
78 */
79 #define NDSLOTTYPE u_long
80
81 /*
82 * This struct is copy-on-write and allocated from an SMR zone.
83 * All fields are constant after initialization apart from the reference count.
84 *
85 * Check pwd_* routines for usage.
86 */
87 struct pwd {
88 volatile u_int pwd_refcount;
89 struct vnode *pwd_cdir; /* current directory */
90 struct vnode *pwd_rdir; /* root directory */
91 struct vnode *pwd_jdir; /* jail root directory */
92 };
93 typedef SMR_POINTER(struct pwd *) smrpwd_t;
94
95 struct pwddesc {
96 struct mtx pd_lock; /* protects members of this struct */
97 smrpwd_t pd_pwd; /* directories */
98 volatile u_int pd_refcount;
99 u_short pd_cmask; /* mask for file creation */
100 };
101
102 struct filedesc {
103 struct fdescenttbl *fd_files; /* open files table */
104 NDSLOTTYPE *fd_map; /* bitmap of free fds */
105 int fd_freefile; /* approx. next free file */
106 int fd_refcnt; /* thread reference count */
107 int fd_holdcnt; /* hold count on structure + mutex */
108 struct sx fd_sx; /* protects members of this struct */
109 struct kqlist fd_kqlist; /* list of kqueues on this filedesc */
110 int fd_holdleaderscount; /* block fdfree() for shared close() */
111 int fd_holdleaderswakeup; /* fdfree() needs wakeup */
112 };
113
114 /*
115 * Structure to keep track of (process leader, struct fildedesc) tuples.
116 * Each process has a pointer to such a structure when detailed tracking
117 * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
118 * table to be shared by processes having different "p_leader" pointers
119 * and thus distinct POSIX style locks.
120 *
121 * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
122 */
123 struct filedesc_to_leader {
124 int fdl_refcount; /* references from struct proc */
125 int fdl_holdcount; /* temporary hold during closef */
126 int fdl_wakeup; /* fdfree() waits on closef() */
127 struct proc *fdl_leader; /* owner of POSIX locks */
128 /* Circular list: */
129 struct filedesc_to_leader *fdl_prev;
130 struct filedesc_to_leader *fdl_next;
131 };
132 #define fd_nfiles fd_files->fdt_nfiles
133 #define fd_ofiles fd_files->fdt_ofiles
134
135 /*
136 * Per-process open flags.
137 */
138 #define UF_EXCLOSE 0x01 /* auto-close on exec */
139
140 #ifdef _KERNEL
141
142 /* Lock a paths descriptor table. */
143 #define PWDDESC_LOCK(pdp) (&(pdp)->pd_lock)
144 #define PWDDESC_LOCK_INIT(pdp) \
145 mtx_init(PWDDESC_LOCK(pdp), "pwddesc", NULL, MTX_DEF)
146 #define PWDDESC_LOCK_DESTROY(pdp) mtx_destroy(PWDDESC_LOCK(pdp))
147 #define PWDDESC_XLOCK(pdp) mtx_lock(PWDDESC_LOCK(pdp))
148 #define PWDDESC_XUNLOCK(pdp) mtx_unlock(PWDDESC_LOCK(pdp))
149 #define PWDDESC_LOCK_ASSERT(pdp, what) \
150 mtx_assert(PWDDESC_LOCK(pdp), (what))
151 #define PWDDESC_ASSERT_XLOCKED(pdp) \
152 PWDDESC_LOCK_ASSERT((pdp), MA_OWNED)
153 #define PWDDESC_ASSERT_UNLOCKED(pdp) \
154 PWDDESC_LOCK_ASSERT((pdp), MA_NOTOWNED)
155
156 #define PWDDESC_XLOCKED_LOAD_PWD(pdp) ({ \
157 struct pwddesc *_pdp = (pdp); \
158 struct pwd *_pwd; \
159 _pwd = smr_serialized_load(&(_pdp)->pd_pwd, \
160 (PWDDESC_ASSERT_XLOCKED(_pdp), true)); \
161 _pwd; \
162 })
163
164 /* Lock a file descriptor table. */
165 #define FILEDESC_LOCK_INIT(fdp) sx_init(&(fdp)->fd_sx, "filedesc structure")
166 #define FILEDESC_LOCK_DESTROY(fdp) sx_destroy(&(fdp)->fd_sx)
167 #define FILEDESC_LOCK(fdp) (&(fdp)->fd_sx)
168 #define FILEDESC_XLOCK(fdp) sx_xlock(&(fdp)->fd_sx)
169 #define FILEDESC_XUNLOCK(fdp) sx_xunlock(&(fdp)->fd_sx)
170 #define FILEDESC_SLOCK(fdp) sx_slock(&(fdp)->fd_sx)
171 #define FILEDESC_SUNLOCK(fdp) sx_sunlock(&(fdp)->fd_sx)
172
173 #define FILEDESC_LOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
174 SX_NOTRECURSED)
175 #define FILEDESC_XLOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
176 SX_NOTRECURSED)
177 #define FILEDESC_UNLOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
178
179 #define FILEDESC_IS_ONLY_USER(fdp) ({ \
180 struct filedesc *_fdp = (fdp); \
181 MPASS(curproc->p_fd == _fdp); \
182 (curproc->p_numthreads == 1 && refcount_load(&_fdp->fd_refcnt) == 1); \
183 })
184 #else
185
186 /*
187 * Accessor for libkvm et al.
188 */
189 #define PWDDESC_KVM_LOAD_PWD(pdp) ({ \
190 struct pwddesc *_pdp = (pdp); \
191 struct pwd *_pwd; \
192 _pwd = smr_kvm_load(&(_pdp)->pd_pwd); \
193 _pwd; \
194 })
195
196 #endif
197
198 #ifdef _KERNEL
199
200 /* Operation types for kern_dup(). */
201 enum {
202 FDDUP_NORMAL, /* dup() behavior. */
203 FDDUP_FCNTL, /* fcntl()-style errors. */
204 FDDUP_FIXED, /* Force fixed allocation. */
205 FDDUP_MUSTREPLACE, /* Target must exist. */
206 FDDUP_LASTMODE,
207 };
208
209 /* Flags for kern_dup(). */
210 #define FDDUP_FLAG_CLOEXEC 0x1 /* Atomically set UF_EXCLOSE. */
211
212 /* For backward compatibility. */
213 #define falloc(td, resultfp, resultfd, flags) \
214 falloc_caps(td, resultfp, resultfd, flags, NULL)
215
216 struct mount;
217 struct thread;
218
219 static __inline void
filecaps_init(struct filecaps * fcaps)220 filecaps_init(struct filecaps *fcaps)
221 {
222
223 bzero(fcaps, sizeof(*fcaps));
224 fcaps->fc_nioctls = -1;
225 }
226 bool filecaps_copy(const struct filecaps *src, struct filecaps *dst,
227 bool locked);
228 void filecaps_move(struct filecaps *src, struct filecaps *dst);
229 void filecaps_free(struct filecaps *fcaps);
230
231 int closef(struct file *fp, struct thread *td);
232 void closef_nothread(struct file *fp);
233 int descrip_check_write_mp(struct filedesc *fdp, struct mount *mp);
234 int dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
235 int openerror, int *indxp);
236 int falloc_caps(struct thread *td, struct file **resultfp, int *resultfd,
237 int flags, struct filecaps *fcaps);
238 void falloc_abort(struct thread *td, struct file *fp);
239 int _falloc_noinstall(struct thread *td, struct file **resultfp, u_int n);
240 #define falloc_noinstall(td, resultfp) _falloc_noinstall(td, resultfp, 1)
241 void _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
242 struct filecaps *fcaps);
243 int finstall(struct thread *td, struct file *fp, int *resultfd, int flags,
244 struct filecaps *fcaps);
245 int finstall_refed(struct thread *td, struct file *fp, int *resultfd, int flags,
246 struct filecaps *fcaps);
247 int fdalloc(struct thread *td, int minfd, int *result);
248 int fdallocn(struct thread *td, int minfd, int *fds, int n);
249 int fdcheckstd(struct thread *td);
250 void fdclose(struct thread *td, struct file *fp, int idx);
251 void fdcloseexec(struct thread *td);
252 void fdsetugidsafety(struct thread *td);
253 struct filedesc *fdcopy(struct filedesc *fdp);
254 int fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds,
255 struct filedesc **newfdp);
256 void fdinstall_remapped(struct thread *td, struct filedesc *fdp);
257 void fdunshare(struct thread *td);
258 void fdescfree(struct thread *td);
259 void fdescfree_remapped(struct filedesc *fdp);
260 int fdlastfile(struct filedesc *fdp);
261 int fdlastfile_single(struct filedesc *fdp);
262 struct filedesc *fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile);
263 struct filedesc *fdshare(struct filedesc *fdp);
264 struct filedesc_to_leader *
265 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
266 struct filedesc *fdp, struct proc *leader);
267 struct filedesc_to_leader *
268 filedesc_to_leader_share(struct filedesc_to_leader *fdtol,
269 struct filedesc *fdp);
270 int getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
271 struct file **fpp);
272 int getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
273 struct file **fpp);
274 void mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
275
276 int fget_cap_locked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
277 struct file **fpp, struct filecaps *havecapsp);
278 int fget_cap(struct thread *td, int fd, cap_rights_t *needrightsp,
279 struct file **fpp, struct filecaps *havecapsp);
280 /* Return a referenced file from an unlocked descriptor. */
281 int fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
282 struct file **fpp);
283 /* Return a file pointer without a ref. FILEDESC_IS_ONLY_USER must be true. */
284 int fget_only_user(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
285 struct file **fpp);
286 #define fput_only_user(fdp, fp) ({ \
287 MPASS(FILEDESC_IS_ONLY_USER(fdp)); \
288 MPASS(refcount_load(&fp->f_count) > 0); \
289 })
290
291 /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
292 static __inline struct file *
fget_locked(struct filedesc * fdp,int fd)293 fget_locked(struct filedesc *fdp, int fd)
294 {
295
296 FILEDESC_LOCK_ASSERT(fdp);
297
298 if (__predict_false((u_int)fd >= fdp->fd_nfiles))
299 return (NULL);
300
301 return (fdp->fd_ofiles[fd].fde_file);
302 }
303
304 static __inline struct filedescent *
fdeget_locked(struct filedesc * fdp,int fd)305 fdeget_locked(struct filedesc *fdp, int fd)
306 {
307 struct filedescent *fde;
308
309 FILEDESC_LOCK_ASSERT(fdp);
310
311 if (__predict_false((u_int)fd >= fdp->fd_nfiles))
312 return (NULL);
313
314 fde = &fdp->fd_ofiles[fd];
315 if (__predict_false(fde->fde_file == NULL))
316 return (NULL);
317
318 return (fde);
319 }
320
321 #ifdef CAPABILITIES
322 static __inline bool
fd_modified(struct filedesc * fdp,int fd,seqc_t seqc)323 fd_modified(struct filedesc *fdp, int fd, seqc_t seqc)
324 {
325
326 return (!seqc_consistent(fd_seqc(fdp->fd_files, fd), seqc));
327 }
328 #endif
329
330 /* cdir/rdir/jdir manipulation functions. */
331 struct pwddesc *pdcopy(struct pwddesc *pdp);
332 void pdescfree(struct thread *td);
333 struct pwddesc *pdinit(struct pwddesc *pdp, bool keeplock);
334 struct pwddesc *pdshare(struct pwddesc *pdp);
335 void pdunshare(struct thread *td);
336
337 void pwd_chdir(struct thread *td, struct vnode *vp);
338 int pwd_chroot(struct thread *td, struct vnode *vp);
339 int pwd_chroot_chdir(struct thread *td, struct vnode *vp);
340 void pwd_ensure_dirs(void);
341 void pwd_set_rootvnode(void);
342
343 struct pwd *pwd_hold_pwddesc(struct pwddesc *pdp);
344 bool pwd_hold_smr(struct pwd *pwd);
345 struct pwd *pwd_hold_proc(struct proc *p);
346 struct pwd *pwd_hold(struct thread *td);
347 void pwd_drop(struct pwd *pwd);
348 static inline void
pwd_set(struct pwddesc * pdp,struct pwd * newpwd)349 pwd_set(struct pwddesc *pdp, struct pwd *newpwd)
350 {
351 smr_serialized_store(&pdp->pd_pwd, newpwd,
352 (PWDDESC_ASSERT_XLOCKED(pdp), true));
353 }
354 #define pwd_get_smr() vfs_smr_entered_load(&curproc->p_pd->pd_pwd)
355
356 #endif /* _KERNEL */
357
358 #endif /* !_SYS_FILEDESC_H_ */
359