FreeBSD/sys/kern/kern_priv.c
/*
 * Check a credential for privilege.  Lots of good reasons to deny privilege;
 * only a few to grant it.
 */
Check a credential for privilege.  Lots of good reasons to deny privilege;
only a few to grant it.
Check a credential for privilege . Lots of good reasons to deny privilege ; only a few to grant it .
int
priv_check_cred(struct ucred *cred, int priv, int flags)
{
	int error;
	KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d",
	    priv));
#ifdef MAC
	error = mac_priv_check(cred, priv);
	if (error)
		goto out;
#endif
	error = prison_priv_check(cred, priv);
	if (error)
		goto out;
	if (unprivileged_mlock) {
		switch (priv) {
		case PRIV_VM_MLOCK:
		case PRIV_VM_MUNLOCK:
			error = 0;
			goto out;
		}
	}
	if (suser_enabled) {
		switch (priv) {
		case PRIV_MAXFILES:
		case PRIV_MAXPROC:
		case PRIV_PROC_LIMIT:
			if (cred->cr_ruid == 0) {
				error = 0;
				goto out;
			}
			break;
		default:
			if (cred->cr_uid == 0) {
				error = 0;
				goto out;
			}
			break;
		}
	}
	if (priv == PRIV_KMEM_READ) {
		error = 0;
		goto out;
	}
#ifdef MAC
	if (mac_priv_grant(cred, priv) == 0) {
		error = 0;
		goto out;
	}
#endif
	error = EPERM;
out:
	if (error)
		SDT_PROBE1(priv, kernel, priv_check, priv__err, priv);
	else
		SDT_PROBE1(priv, kernel, priv_check, priv__ok, priv);
	return (error);
}
int priv_check_cred ( struct ucred * cred , int priv , int flags ) { int error ; KASSERT ( PRIV_VALID ( priv ) , ( " " , priv ) ) ; #ifdef MAC error = mac_priv_check ( cred , priv ) ; if ( error ) goto out ; #endif error = prison_priv_check ( cred , priv ) ; if ( error ) goto out ; if ( unprivileged_mlock ) { switch ( priv ) { case PRIV_VM_MLOCK : case PRIV_VM_MUNLOCK : error = 0 ; goto out ; } } if ( suser_enabled ) { switch ( priv ) { case PRIV_MAXFILES : case PRIV_MAXPROC : case PRIV_PROC_LIMIT : if ( cred -> cr_ruid == 0 ) { error = 0 ; goto out ; } break ; default : if ( cred -> cr_uid == 0 ) { error = 0 ; goto out ; } break ; } } if ( priv == PRIV_KMEM_READ ) { error = 0 ; goto out ; } #ifdef MAC if ( mac_priv_grant ( cred , priv ) == 0 ) { error = 0 ; goto out ; } #endif error = EPERM ; out : if ( error ) SDT_PROBE1 ( priv , kernel , priv_check , priv__err , priv ) ; else SDT_PROBE1 ( priv , kernel , priv_check , priv__ok , priv ) ; return ( error ) ; }
Check a credential for privilege.
Check a credential for privilege .
/*
	 * We first evaluate policies that may deny the granting of
	 * privilege unilaterally.
	 */ /*
	 * Jail policy will restrict certain privileges that may otherwise be
	 * be granted.
	 */ /*
		 * Allow unprivileged users to call mlock(2)/munlock(2) and
		 * mlockall(2)/munlockall(2).
		 */ /*
	 * Having determined if privilege is restricted by various policies,
	 * now determine if privilege is granted.  At this point, any policy
	 * may grant privilege.  For now, we allow short-circuit boolean
	 * evaluation, so may not call all policies.  Perhaps we should.
	 *
	 * Superuser policy grants privilege based on the effective (or in
	 * the case of specific privileges, real) uid being 0.  We allow the
	 * superuser policy to be globally disabled, although this is
	 * currenty of limited utility.
	 */ /*
	 * Writes to kernel/physical memory are a typical root-only operation,
	 * but non-root users are expected to be able to read it (provided they
	 * have permission to access /dev/[k]mem).
	 */ /*
	 * Now check with MAC, if enabled, to see if a policy module grants
	 * privilege.
	 */ /*
	 * The default is deny, so if no policies have granted it, reject
	 * with a privilege error here.
	 */