Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * Copyright (c) 2015-2022 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
#include <sys/proc_internal.h>
#include <sys/file_internal.h>
#include <sys/vnode_internal.h>
#include <sys/sysproto.h>
#include <security/audit/audit.h>
#include <skywalk/os_skywalk_private.h>

static int nxop_ioctl(struct fileproc *, u_long, caddr_t, vfs_context_t);
static int nxop_close(struct fileglob *, vfs_context_t);

static const struct fileops nexus_ctl_ops = {
	.fo_type     = DTYPE_NEXUS,
	.fo_read     = fo_no_read,
	.fo_write    = fo_no_write,
	.fo_ioctl    = nxop_ioctl,
	.fo_select   = fo_no_select,
	.fo_close    = nxop_close,
	.fo_drain    = fo_no_drain,
	.fo_kqfilter = fo_no_kqfilter,
};

static int
nxop_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx)
{
	struct nxctl *nxctl;
	proc_t procp = vfs_context_proc(ctx);

	if ((nxctl = (struct nxctl *)fp_get_data(fp)) == NULL) {
		/* This is not a valid open file descriptor */
		return EBADF;
	}
	return nxioctl(nxctl, cmd, data, procp);
}

static int
nxop_close(struct fileglob *fg, vfs_context_t ctx)
{
#pragma unused(ctx)
	struct nxctl *nxctl;
	int error = 0;

	nxctl = (struct nxctl *)fg_get_data(fg);
	fg_set_data(fg, NULL);
	if (nxctl != NULL) {
		nxctl_dtor(nxctl);
	}

	return error;
}

int
__nexus_open(struct proc *p, struct __nexus_open_args *uap, int *retval)
{
	struct nxctl *nxctl = NULL;
	struct fileproc *__single fp = NULL;
	struct nxctl_init init;
	uuid_t nxctl_uuid;
	int fd = -1, err = 0;
	guardid_t guard;

	if (__improbable(uap->init == USER_ADDR_NULL ||
	    uap->init_len < sizeof(init))) {
		SK_DSC(p, "EINVAL: init %p, init_len %u", uap->init,
		    uap->init_len);
		err = EINVAL;
		goto done;
	}

	err = copyin(uap->init, (caddr_t)&init, sizeof(init));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyin err %d, init 0x%llx", err, SK_KVA(uap->init));
		goto done;
	}

	if (__improbable(init.ni_version != NEXUSCTL_INIT_CURRENT_VERSION)) {
		SK_DSC(p, "ENOTSUP: version %u != %u", init.ni_version,
		    NEXUSCTL_INIT_CURRENT_VERSION);
		err = ENOTSUP;
		goto done;
	}

	/* generate guard ID based on nexus controller UUID */
	uuid_generate_random(nxctl_uuid);
	sk_gen_guard_id(FALSE, nxctl_uuid, &guard);

	err = falloc_guarded(p, &fp, &fd, vfs_context_current(), &guard,
	    GUARD_CLOSE | GUARD_DUP | GUARD_SOCKET_IPC | GUARD_FILEPORT | GUARD_WRITE);
	if (__improbable(err != 0)) {
		SK_DSC(p, "falloc_guarded err %d", err);
		goto done;
	}

	nxctl = nxctl_create(p, fp, nxctl_uuid, &err);
	if (__improbable(nxctl == NULL)) {
		ASSERT(err != 0);
		SK_DSC(p, "nxctl_create err %d", err);
		goto done;
	}

	/* update userland with respect to guard ID, etc. */
	init.ni_guard = guard;
	err = copyout(&init, uap->init, sizeof(init));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyout err %d, init 0x%llx", err,
		    SK_KVA(uap->init));
		goto done;
	}

	fp->fp_flags |= FP_CLOEXEC | FP_CLOFORK;
	fp->fp_glob->fg_flag |= (FREAD | FWRITE);
	fp->fp_glob->fg_ops = &nexus_ctl_ops;
	fp_set_data(fp, nxctl);   /* ref from nxctl_create */

	proc_fdlock(p);
	procfdtbl_releasefd(p, fd, NULL);
	fp_drop(p, fd, fp, 1);
	proc_fdunlock(p);

	*retval = fd;

	SK_D("%s(%d) fd %d guard 0x%llx",
	    sk_proc_name_address(p), sk_proc_pid(p), fd, guard);

done:
	if (__improbable(err != 0)) {
		if (nxctl != NULL) {
			nxctl_dtor(nxctl);
			nxctl = NULL;
		}
		if (fp != NULL) {
			fp_free(p, fd, fp);
			fp = NULL;
		}
	}

	return err;
}

int
__nexus_register(struct proc *p, struct __nexus_register_args *uap, int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct kern_nexus_provider *nxprov = NULL;
	struct nxctl *nxctl;
	struct nxprov_reg reg;
	int err = 0;

	AUDIT_ARG(fd, uap->ctl);

	if (__improbable(uap->reg == USER_ADDR_NULL ||
	    uap->reg_len < sizeof(reg) || uap->prov_uuid == USER_ADDR_NULL ||
	    uap->prov_uuid_len < sizeof(uuid_t))) {
		SK_DSC(p, "EINVAL: reg 0x%llx, reg_len %u, prov_uuid 0x%llx, "
		    "prov_uuid_len %u", SK_KVA(uap->reg), uap->reg_len,
		    SK_KVA(uap->prov_uuid), uap->prov_uuid_len);
		return EINVAL;
	}

	err = copyin(uap->reg, (caddr_t)&reg, sizeof(reg));
	if (err != 0) {
		SK_DSC(p, "copyin err %d, reg 0x%llx", err, SK_KVA(uap->reg));
		return err;
	}

	if (__improbable(reg.nxpreg_version != NXPROV_REG_CURRENT_VERSION)) {
		SK_DSC(p, "EINVAL: version %u != %u", reg.nxpreg_version,
		    NXPROV_REG_CURRENT_VERSION);
		return EINVAL;
	}

	if (__improbable(reg.nxpreg_params.nxp_namelen == 0 ||
	    reg.nxpreg_params.nxp_namelen > sizeof(nexus_name_t))) {
		SK_DSC(p, "EINVAL: namelen %u", reg.nxpreg_params.nxp_namelen);
		return EINVAL;
	}

	err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
	if (__improbable(err != 0)) {
		SK_DSC(p, "fp_get_ftype: %d", err);
		return err;
	}
	nxctl = (struct nxctl *)fp_get_data(fp);

	lck_mtx_lock(&nxctl->nxctl_lock);
	nxprov = nxprov_create(p, nxctl, &reg, &err);
	lck_mtx_unlock(&nxctl->nxctl_lock);
	if (__improbable(nxprov == NULL)) {
		ASSERT(err != 0);
		SK_DSC(p, "nxprov_create: %d", err);
		goto done;
	}

	err = copyout(&nxprov->nxprov_uuid, uap->prov_uuid, sizeof(uuid_t));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyout err %d, prov_uuid 0x%llx", err,
		    SK_KVA(uap->prov_uuid));
		goto done;
	}

done:
	fp_drop(p, uap->ctl, fp, 0);

	if (__improbable(err != 0 && nxprov != NULL)) {
		err = nxprov_close(nxprov, FALSE);
	}

	/* release extra ref from nxprov_create */
	if (nxprov != NULL) {
		nxprov_release(nxprov);
	}

	return err;
}

int
__nexus_deregister(struct proc *p, struct __nexus_deregister_args *uap,
    int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct nxctl *nxctl = NULL;
	uuid_t nxprov_uuid;
	int err = 0;

	AUDIT_ARG(fd, uap->ctl);

	if (__improbable(uap->prov_uuid_len < sizeof(uuid_t))) {
		SK_DSC(p, "EINVAL: prov_len %u < %u", uap->prov_uuid_len,
		    sizeof(uuid_t));
		return EINVAL;
	}

	err = copyin(uap->prov_uuid, (caddr_t)&nxprov_uuid, sizeof(uuid_t));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyin err %d, prov_uuid 0x%llx", err,
		    SK_KVA(uap->prov_uuid));
		return err;
	}

	if (__improbable(uuid_is_null(nxprov_uuid))) {
		SK_DSC(p, "EINVAL: uuid_is_null");
		return EINVAL;
	}

	err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
	if (__improbable(err != 0)) {
		SK_DSC(p, "fp_get_ftype: %d", err);
		return err;
	}
	nxctl = (struct nxctl *)fp_get_data(fp);

	lck_mtx_lock(&nxctl->nxctl_lock);
	err = nxprov_destroy(nxctl, nxprov_uuid);
	lck_mtx_unlock(&nxctl->nxctl_lock);

	fp_drop(p, uap->ctl, fp, 0);

	return err;
}

int
__nexus_create(struct proc *p, struct __nexus_create_args *uap, int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct kern_nexus *nx = NULL;
	struct nxctl *nxctl = NULL;
	uuid_t nxprov_uuid;
	int err = 0;

	AUDIT_ARG(fd, uap->ctl);

	if (__improbable(uap->prov_uuid_len < sizeof(uuid_t) ||
	    uap->nx_uuid_len < sizeof(uuid_t) ||
	    uap->nx_uuid == USER_ADDR_NULL)) {
		SK_DSC(p, "EINVAL: prov_uuid_len %u, nx_uuid_len %u, "
		    "nx_uuid 0x%llx", uap->prov_uuid_len, uap->nx_uuid_len,
		    SK_KVA(uap->nx_uuid));
		return EINVAL;
	}

	err = copyin(uap->prov_uuid, (caddr_t)&nxprov_uuid, sizeof(uuid_t));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyin err %d, prov_uuid 0x%llx", err,
		    SK_KVA(uap->prov_uuid));
		return err;
	}

	if (__improbable(uuid_is_null(nxprov_uuid))) {
		SK_DSC(p, "EINVAL: uuid_is_null");
		return EINVAL;
	}

	err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
	if (__improbable(err != 0)) {
		SK_DSC(p, "fp_get_ftype: %d", err);
		return err;
	}
	nxctl = (struct nxctl *)fp_get_data(fp);

	lck_mtx_lock(&nxctl->nxctl_lock);
	nx = nx_create(nxctl, nxprov_uuid, NEXUS_TYPE_UNDEFINED, NULL, NULL,
	    NULL, NULL, &err);
	lck_mtx_unlock(&nxctl->nxctl_lock);
	if (__improbable(nx == NULL)) {
		ASSERT(err != 0);
		SK_DSC(p, "nx_create: %d", err);
		goto done;
	}
	err = copyout(&nx->nx_uuid, uap->nx_uuid, sizeof(uuid_t));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyout err %d, nx_uuid 0x%llx", err,
		    SK_KVA(uap->nx_uuid));
		goto done;
	}

done:
	fp_drop(p, uap->ctl, fp, 0);

	/* release extra ref from nx_create */
	if (nx != NULL) {
		(void) nx_release(nx);
	}

	return err;
}

int
__nexus_destroy(struct proc *p, struct __nexus_destroy_args *uap, int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct nxctl *nxctl = NULL;
	int err = 0;
	uuid_t nx_uuid;

	AUDIT_ARG(fd, uap->ctl);

	if (__improbable(uap->nx_uuid == USER_ADDR_NULL ||
	    uap->nx_uuid_len < sizeof(uuid_t))) {
		SK_DSC(p, "EINVAL: nx_uuid 0x%llx, nx_uuid_len %u",
		    SK_KVA(uap->nx_uuid), uap->nx_uuid_len);
		return EINVAL;
	}

	err = copyin(uap->nx_uuid, (caddr_t)&nx_uuid, sizeof(uuid_t));
	if (__improbable(err != 0)) {
		SK_DSC(p, "copyin err %d, nx_uuid 0x%llx", err,
		    SK_KVA(uap->nx_uuid));
		return err;
	}

	if (__improbable(uuid_is_null(nx_uuid))) {
		SK_DSC(p, "EINVAL: uuid_is_null");
		return EINVAL;
	}

	err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
	if (__improbable(err != 0)) {
		SK_DSC(p, "fp_get_ftype: %d", err);
		return err;
	}
	nxctl = (struct nxctl *)fp_get_data(fp);

	lck_mtx_lock(&nxctl->nxctl_lock);
	err = nx_destroy(nxctl, nx_uuid);
	lck_mtx_unlock(&nxctl->nxctl_lock);

	fp_drop(p, uap->ctl, fp, 0);

	return err;
}

int
__nexus_get_opt(struct proc *p, struct __nexus_get_opt_args *uap, int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct nxctl *nxctl = NULL;
	struct sockopt sopt;
	uint32_t optlen;
	int err = 0;

	AUDIT_ARG(fd, uap->ctl);

	err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
	if (__improbable(err != 0)) {
		SK_DSC(p, "fp_get_ftype: %d", err);
		return err;
	}
	nxctl = (struct nxctl *)fp_get_data(fp);

	if (__improbable(uap->aoptlen == USER_ADDR_NULL)) {
		SK_DSC(p, "EINVAL: aoptlen == USER_ADDR_NULL");
		err = EINVAL;
		goto done;
	}

	if (uap->aoptval != USER_ADDR_NULL) {
		err = copyin(uap->aoptlen, &optlen, sizeof(optlen));
		if (__improbable(err != 0)) {
			SK_DSC(p, "copyin err %d, aoptlen 0x%llx", err,
			    SK_KVA(uap->aoptlen));
			goto done;
		}
	} else {
		optlen = 0;
	}

	bzero(&sopt, sizeof(sopt));
	sopt.sopt_dir = SOPT_GET;
	sopt.sopt_name = uap->opt;
	sopt.sopt_val = uap->aoptval;
	sopt.sopt_valsize = optlen;
	sopt.sopt_p = p;

	lck_mtx_lock(&nxctl->nxctl_lock);
	err = nxctl_get_opt(nxctl, &sopt);
	lck_mtx_unlock(&nxctl->nxctl_lock);
	if (__probable(err == 0)) {
		optlen = (uint32_t)sopt.sopt_valsize;
		err = copyout(&optlen, uap->aoptlen, sizeof(optlen));
#if SK_LOG
		if (__improbable(err != 0)) {
			SK_DSC(p, "copyout err %d, aoptlen 0x%llx", err,
			    SK_KVA(uap->aoptlen));
		}
#endif /* SK_LOG */
	}

done:
	fp_drop(p, uap->ctl, fp, 0);

	return err;
}

int
__nexus_set_opt(struct proc *p, struct __nexus_set_opt_args *uap, int *retval)
{
#pragma unused(retval)
	struct fileproc *__single fp;
	struct nxctl *nxctl = NULL;
	struct sockopt sopt;
	int err = 0;

	bzero(&sopt, sizeof(sopt));
	sopt.sopt_dir = SOPT_SET;
	sopt.sopt_name = uap->opt;
	sopt.sopt_val = uap->aoptval;
	sopt.sopt_valsize = uap->optlen;
	sopt.sopt_p = p;

	if (uap->ctl != __OS_NEXUS_SHARED_USER_CONTROLLER_FD) {
		AUDIT_ARG(fd, uap->ctl);

		err = fp_get_ftype(p, uap->ctl, DTYPE_NEXUS, ENODEV, &fp);
		if (__improbable(err != 0)) {
			SK_DSC(p, "fp_get_ftype: %d", err);
			return err;
		}
		nxctl = (struct nxctl *)fp_get_data(fp);

		lck_mtx_lock(&nxctl->nxctl_lock);
		err = nxctl_set_opt(nxctl, &sopt);
		lck_mtx_unlock(&nxctl->nxctl_lock);

		fp_drop(p, uap->ctl, fp, 0);
	} else {
		/* opt that don't have nxctl uses shared user nxctl */
		nxctl = usernxctl.ncd_nxctl;

		lck_mtx_lock(&nxctl->nxctl_lock);
		err = nxctl_set_opt(nxctl, &sopt);
		lck_mtx_unlock(&nxctl->nxctl_lock);
	}
	return err;
}