+
+/*
+ * gethostuuid
+ *
+ * Description: Get the host UUID from IOKit and return it to user space.
+ *
+ * Parameters: uuid_buf Pointer to buffer to receive UUID
+ * timeout Timespec for timout
+ * spi SPI, skip sandbox check (temporary)
+ *
+ * Returns: 0 Success
+ * EWOULDBLOCK Timeout is too short
+ * copyout:EFAULT Bad user buffer
+ *
+ * Notes: A timeout seems redundant, since if it's tolerable to not
+ * have a system UUID in hand, then why ask for one?
+ */
+int
+gethostuuid(struct proc *p, struct gethostuuid_args *uap, __unused int32_t *retval)
+{
+ kern_return_t kret;
+ int error;
+ mach_timespec_t mach_ts; /* for IOKit call */
+ __darwin_uuid_t uuid_kern; /* for IOKit call */
+
+ if (!uap->spi) {
+#if 13841988
+ uint32_t flags;
+ if (temp_debug_13841988 && (0 == proc_get_darwinbgstate(p->task, &flags)) && (flags & PROC_FLAG_IOS_APPLICATION)) {
+ printf("Unauthorized access to gethostuuid() by %s(%d)\n", p->p_comm, proc_pid(p));
+ return (EPERM);
+ }
+#else
+ /* Perform sandbox check */
+#endif
+ }
+
+ /* Convert the 32/64 bit timespec into a mach_timespec_t */
+ if ( proc_is64bit(p) ) {
+ struct user64_timespec ts;
+ error = copyin(uap->timeoutp, &ts, sizeof(ts));
+ if (error)
+ return (error);
+ mach_ts.tv_sec = ts.tv_sec;
+ mach_ts.tv_nsec = ts.tv_nsec;
+ } else {
+ struct user32_timespec ts;
+ error = copyin(uap->timeoutp, &ts, sizeof(ts) );
+ if (error)
+ return (error);
+ mach_ts.tv_sec = ts.tv_sec;
+ mach_ts.tv_nsec = ts.tv_nsec;
+ }
+
+ /* Call IOKit with the stack buffer to get the UUID */
+ kret = IOBSDGetPlatformUUID(uuid_kern, mach_ts);
+
+ /*
+ * If we get it, copy out the data to the user buffer; note that a
+ * uuid_t is an array of characters, so this is size invariant for
+ * 32 vs. 64 bit.
+ */
+ if (kret == KERN_SUCCESS) {
+ error = copyout(uuid_kern, uap->uuid_buf, sizeof(uuid_kern));
+ } else {
+ error = EWOULDBLOCK;
+ }
+
+ return (error);
+}
+
+/*
+ * ledger
+ *
+ * Description: Omnibus system call for ledger operations
+ */
+int
+ledger(struct proc *p, struct ledger_args *args, __unused int32_t *retval)
+{
+#if !CONFIG_MACF
+#pragma unused(p)
+#endif
+ int rval, pid, len, error;
+#ifdef LEDGER_DEBUG
+ struct ledger_limit_args lla;
+#endif
+ task_t task;
+ proc_t proc;
+
+ /* Finish copying in the necessary args before taking the proc lock */
+ error = 0;
+ len = 0;
+ if (args->cmd == LEDGER_ENTRY_INFO)
+ error = copyin(args->arg3, (char *)&len, sizeof (len));
+ else if (args->cmd == LEDGER_TEMPLATE_INFO)
+ error = copyin(args->arg2, (char *)&len, sizeof (len));
+#ifdef LEDGER_DEBUG
+ else if (args->cmd == LEDGER_LIMIT)
+ error = copyin(args->arg2, (char *)&lla, sizeof (lla));
+#endif
+ if (error)
+ return (error);
+ if (len < 0)
+ return (EINVAL);
+
+ rval = 0;
+ if (args->cmd != LEDGER_TEMPLATE_INFO) {
+ pid = args->arg1;
+ proc = proc_find(pid);
+ if (proc == NULL)
+ return (ESRCH);
+
+#if CONFIG_MACF
+ error = mac_proc_check_ledger(p, proc, args->cmd);
+ if (error) {
+ proc_rele(proc);
+ return (error);
+ }
+#endif
+
+ task = proc->task;
+ }
+
+ switch (args->cmd) {
+#ifdef LEDGER_DEBUG
+ case LEDGER_LIMIT: {
+ if (!kauth_cred_issuser(kauth_cred_get()))
+ rval = EPERM;
+ rval = ledger_limit(task, &lla);
+ proc_rele(proc);
+ break;
+ }
+#endif
+ case LEDGER_INFO: {
+ struct ledger_info info;
+
+ rval = ledger_info(task, &info);
+ proc_rele(proc);
+ if (rval == 0)
+ rval = copyout(&info, args->arg2,
+ sizeof (info));
+ break;
+ }
+
+ case LEDGER_ENTRY_INFO: {
+ void *buf;
+ int sz;
+
+ rval = ledger_get_task_entry_info_multiple(task, &buf, &len);
+ proc_rele(proc);
+ if ((rval == 0) && (len > 0)) {
+ sz = len * sizeof (struct ledger_entry_info);
+ rval = copyout(buf, args->arg2, sz);
+ kfree(buf, sz);
+ }
+ if (rval == 0)
+ rval = copyout(&len, args->arg3, sizeof (len));
+ break;
+ }
+
+ case LEDGER_TEMPLATE_INFO: {
+ void *buf;
+ int sz;
+
+ rval = ledger_template_info(&buf, &len);
+ if ((rval == 0) && (len > 0)) {
+ sz = len * sizeof (struct ledger_template_info);
+ rval = copyout(buf, args->arg1, sz);
+ kfree(buf, sz);
+ }
+ if (rval == 0)
+ rval = copyout(&len, args->arg2, sizeof (len));
+ break;
+ }
+
+ default:
+ rval = EINVAL;
+ }
+
+ return (rval);
+}
+
+#if CONFIG_TELEMETRY
+int
+telemetry(__unused struct proc *p, struct telemetry_args *args, __unused int32_t *retval)
+{
+ int error = 0;
+
+ switch (args->cmd) {
+ case TELEMETRY_CMD_TIMER_EVENT:
+ error = telemetry_timer_event(args->deadline, args->interval, args->leeway);
+ break;
+ default:
+ error = EINVAL;
+ break;
+ }
+
+ return (error);
+}
+#endif /* CONFIG_TELEMETRY */