+ /* XXX Need to implement these commands by accessing the global
+ * values associated with the commands.
+ */
+ lck_mtx_lock(audit_mtx);
+ switch (uap->cmd) {
+ case A_GETPOLICY:
+ if (!audit_fail_stop)
+ udata.au_policy |= AUDIT_CNT;
+ if (audit_panic_on_write_fail)
+ udata.au_policy |= AUDIT_AHLT;
+ break;
+ case A_SETPOLICY:
+ if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT)) {
+ ret = EINVAL;
+ break;
+ }
+ /*
+ * XXX - Need to wake up waiters if the policy relaxes?
+ */
+ audit_fail_stop = ((udata.au_policy & AUDIT_CNT) == 0);
+ audit_panic_on_write_fail = (udata.au_policy & AUDIT_AHLT);
+ break;
+ case A_GETKMASK:
+ udata.au_mask = audit_nae_mask;
+ break;
+ case A_SETKMASK:
+ audit_nae_mask = udata.au_mask;
+ break;
+ case A_GETQCTRL:
+ udata.au_qctrl = audit_qctrl;
+ break;
+ case A_SETQCTRL:
+ if ((udata.au_qctrl.aq_hiwater > AQ_MAXHIGH) ||
+ (udata.au_qctrl.aq_lowater >= udata.au_qctrl.aq_hiwater) ||
+ (udata.au_qctrl.aq_bufsz > AQ_MAXBUFSZ) ||
+ (udata.au_qctrl.aq_minfree < 0) ||
+ (udata.au_qctrl.aq_minfree > 100)) {
+ ret = EINVAL;
+ break;
+ }
+
+ audit_qctrl = udata.au_qctrl;
+ /* XXX The queue delay value isn't used with the kernel. */
+ audit_qctrl.aq_delay = -1;
+ break;
+ case A_GETCWD:
+ ret = ENOSYS;
+ break;
+ case A_GETCAR:
+ ret = ENOSYS;
+ break;
+ case A_GETSTAT:
+ ret = ENOSYS;
+ break;
+ case A_SETSTAT:
+ ret = ENOSYS;
+ break;
+ case A_SETUMASK:
+ ret = ENOSYS;
+ break;
+ case A_SETSMASK:
+ ret = ENOSYS;
+ break;
+ case A_GETCOND:
+ if (audit_enabled && !audit_suspended)
+ udata.au_cond = AUC_AUDITING;
+ else
+ udata.au_cond = AUC_NOAUDIT;
+ break;
+ case A_SETCOND:
+ if (udata.au_cond == AUC_NOAUDIT)
+ audit_suspended = 1;
+ if (udata.au_cond == AUC_AUDITING)
+ audit_suspended = 0;
+ if (udata.au_cond == AUC_DISABLED) {
+ audit_suspended = 1;
+ audit_shutdown();
+ }
+ break;
+ case A_GETCLASS:
+ udata.au_evclass.ec_class =
+ au_event_class(udata.au_evclass.ec_number);
+ break;
+ case A_SETCLASS:
+ au_evclassmap_insert(udata.au_evclass.ec_number,
+ udata.au_evclass.ec_class);
+ break;
+ case A_GETPINFO:
+ if (udata.au_aupinfo.ap_pid < 1) {
+ ret = EINVAL;
+ break;
+ }
+ if ((tp = proc_find(udata.au_aupinfo.ap_pid)) == NULL) {
+ ret = EINVAL;
+ break;
+ }
+
+ lck_mtx_unlock(audit_mtx);
+ my_cred = kauth_cred_proc_ref(tp);
+
+ udata.au_aupinfo.ap_auid = my_cred->cr_au.ai_auid;
+ udata.au_aupinfo.ap_mask.am_success =
+ my_cred->cr_au.ai_mask.am_success;
+ udata.au_aupinfo.ap_mask.am_failure =
+ my_cred->cr_au.ai_mask.am_failure;
+ udata.au_aupinfo.ap_termid.machine =
+ my_cred->cr_au.ai_termid.machine;
+ udata.au_aupinfo.ap_termid.port =
+ my_cred->cr_au.ai_termid.port;
+ udata.au_aupinfo.ap_asid = my_cred->cr_au.ai_asid;
+
+ kauth_cred_unref(&my_cred);
+
+ proc_rele(tp);
+ tp = PROC_NULL;
+ lck_mtx_lock(audit_mtx);
+ break;
+ case A_SETPMASK:
+ if (udata.au_aupinfo.ap_pid < 1) {
+ ret = EINVAL;
+ break;
+ }
+ if ((tp = proc_find(udata.au_aupinfo.ap_pid)) == NULL) {
+ ret = EINVAL;
+ break;
+ }
+
+ /*
+ * we are modifying the audit info in a credential so we need a new
+ * credential (or take another reference on an existing credential that
+ * matches our new one). We must do this because the audit info in the
+ * credential is used as part of our hash key. Get current credential
+ * in the target process and take a reference while we muck with it.
+ */
+ lck_mtx_unlock(audit_mtx);
+ for (;;) {
+ kauth_cred_t my_new_cred;
+ struct auditinfo temp_auditinfo;
+
+ my_cred = kauth_cred_proc_ref(tp);
+ /*
+ * Set the credential with new info. If there is no
+ * change, we get back the same credential we passed
+ * in; if there is a change, we drop the reference on
+ * the credential we passed in. The subsequent
+ * compare is safe, because it is a pointer compare
+ * rather than a contents compare.
+ */
+ temp_auditinfo = my_cred->cr_au;
+ temp_auditinfo.ai_mask.am_success =
+ udata.au_aupinfo.ap_mask.am_success;
+ temp_auditinfo.ai_mask.am_failure =
+ udata.au_aupinfo.ap_mask.am_failure;
+ my_new_cred = kauth_cred_setauditinfo(my_cred, &temp_auditinfo);
+
+ if (my_cred != my_new_cred) {
+ proc_lock(tp);
+ /* need to protect for a race where another thread also changed
+ * the credential after we took our reference. If p_ucred has
+ * changed then we should restart this again with the new cred.
+ */
+ if (tp->p_ucred != my_cred) {
+ proc_unlock(tp);
+ kauth_cred_unref(&my_new_cred);
+ /* try again */
+ continue;
+ }
+ tp->p_ucred = my_new_cred;
+ proc_unlock(tp);
+ }
+ /* drop old proc reference or our extra reference */
+ kauth_cred_unref(&my_cred);
+ break;
+ }
+ proc_rele(tp);
+ lck_mtx_lock(audit_mtx);
+ break;
+ case A_SETFSIZE:
+ if ((udata.au_fstat.af_filesz != 0) &&
+ (udata.au_fstat.af_filesz < MIN_AUDIT_FILE_SIZE)) {
+ ret = EINVAL;
+ break;
+ }
+ audit_fstat.af_filesz = udata.au_fstat.af_filesz;
+ break;
+ case A_GETFSIZE:
+ udata.au_fstat.af_filesz = audit_fstat.af_filesz;
+ udata.au_fstat.af_currsz = audit_fstat.af_currsz;
+ break;
+ case A_GETPINFO_ADDR:
+ ret = ENOSYS;
+ break;
+ case A_GETKAUDIT:
+ ret = ENOSYS;
+ break;
+ case A_SETKAUDIT:
+ ret = ENOSYS;
+ break;
+ }
+ /* Copy data back to userspace for the GET comands */
+ if (ret == 0) {
+ switch (uap->cmd) {
+ case A_GETPOLICY:
+ case A_GETKMASK:
+ case A_GETQCTRL:
+ case A_GETCWD:
+ case A_GETCAR:
+ case A_GETSTAT:
+ case A_GETCOND:
+ case A_GETCLASS:
+ case A_GETPINFO:
+ case A_GETFSIZE:
+ case A_GETPINFO_ADDR:
+ case A_GETKAUDIT:
+ ret = copyout((void *)&udata, uap->data, uap->length);
+ break;
+ }
+ }
+
+ lck_mtx_unlock(audit_mtx);
+ return (ret);