xref: /xnu-11215.41.3/bsd/tests/vm_parameter_validation_kern_bsd.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 #include <sys/cdefs.h>
2 #include <sys/proc_internal.h>
3 #include <sys/vnode_internal.h>
4 #include <sys/ubc_internal.h>
5 #include <sys/file_internal.h>
6 #include <sys/vnode.h>
7 
8 // From vm_parameter_validation_kern.c
9 void testprintf(const char *format, ...);
10 
11 struct file_control_return {
12 	void * control;
13 	struct fileproc * fp;
14 	struct vnode * vp;
15 	int fd;
16 };
17 struct file_control_return get_control_from_fd(int fd);
18 
19 struct file_control_return
get_control_from_fd(int fd)20 get_control_from_fd(int fd)
21 {
22 	struct fileproc                 *fp = NULL;
23 	struct vnode                    *vp = NULL;
24 	size_t                          file_size;
25 	off_t                           fs;
26 	memory_object_control_t         file_control = NULL;
27 	int                             error;
28 	struct file_control_return ret = {NULL, NULL, NULL, fd};
29 
30 
31 	proc_t p = current_proc();
32 
33 	/* get file structure from file descriptor */
34 	error = fp_get_ftype(p, fd, DTYPE_VNODE, EINVAL, &fp);
35 	if (error) {
36 		testprintf("%s: [%d(%s)]: fp_get_ftype() failed, error %d\n",
37 		    __func__, proc_getpid(p), p->p_comm, error);
38 		return ret;
39 	}
40 	ret.fp = fp;
41 
42 	/* We need at least read permission on the file */
43 	if (!(fp->fp_glob->fg_flag & FREAD)) {
44 		testprintf("%s: [%d(%s)]: not readable\n",
45 		    __func__, proc_getpid(p), p->p_comm);
46 		return ret;
47 	}
48 
49 	/* Get the vnode from file structure */
50 	vp = (struct vnode *)fp_get_data(fp);
51 	error = vnode_getwithref(vp);
52 	if (error) {
53 		testprintf("%s: [%d(%s)]: failed to get vnode, error %d\n",
54 		    __func__, proc_getpid(p), p->p_comm, error);
55 		return ret;
56 	}
57 	ret.vp = vp;
58 
59 	/* Make sure the vnode is a regular file */
60 	if (vp->v_type != VREG) {
61 		testprintf("%s: [%d(%s)]: vnode not VREG\n",
62 		    __func__, proc_getpid(p), p->p_comm);
63 		return ret;
64 	}
65 
66 	/* get vnode size */
67 	error = vnode_size(vp, &fs, vfs_context_current());
68 	if (error) {
69 		return ret;
70 	}
71 	file_size = fs;
72 
73 	/* get the file's memory object handle */
74 	file_control = ubc_getobject(vp, UBC_HOLDOBJECT);
75 	if (file_control == MEMORY_OBJECT_CONTROL_NULL) {
76 		testprintf("%s: [%d(%s)]: no memory object\n",
77 		    __func__, proc_getpid(p), p->p_comm);
78 		return ret;
79 	}
80 	ret.control = file_control;
81 
82 	return ret;
83 }
84 
85 void cleanup_control_related_data(struct file_control_return info);
86 
87 void
cleanup_control_related_data(struct file_control_return info)88 cleanup_control_related_data(struct file_control_return info)
89 {
90 	if (info.fp != NULL) {
91 		/* release the file descriptor */
92 		fp_drop(current_proc(), info.fd, info.fp, 0);
93 	}
94 	if (info.vp != NULL) {
95 		(void)vnode_put(info.vp);
96 	}
97 }
98