Linux 內核源碼分析 -- getuid, geteuid


Linux 內核源碼分析 -- getuid,geteuid

getuid

獲取用戶標識號

via:https://man7.org/linux/man-pages/man2/geteuid.2.html

returns the real user ID of the calling process.

SYSCALL_DEFINE0(getuid)
{
	/* Only we change this so SMP safe */
	return from_kuid_munged(current_user_ns(), current_uid());
}

current_user_ns()

一個宏,用來獲取當前進程的 cred

#define current_user_ns()	(current_cred_xxx(user_ns))
#define current_cred_xxx(xxx)			\
({						\
	current_cred()->xxx;			\
})
/**
 * current_cred - Access the current task's subjective credentials
 *
 * Access the subjective credentials of the current task.  RCU-safe,
 * since nobody else can modify it.
 */
#define current_cred() \
	rcu_dereference_protected(current->cred, 1)

current_uid()

獲取進程的 cred->uid

#define current_uid()		(current_cred_xxx(uid))
#define current_cred_xxx(xxx)			\
({						\
	current_cred()->xxx;			\
})
/**
 * current_cred - Access the current task's subjective credentials
 *
 * Access the subjective credentials of the current task.  RCU-safe,
 * since nobody else can modify it.
 */
#define current_cred() \
	rcu_dereference_protected(current->cred, 1)

其實跟上面的 current_user_ns() 差不多,就是用 current_cred_xxx() 拼接

current_cred() 獲取當前進程的 cred

展開宏就是 :current->cred->uid

from_kuid_munged()

uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
{
	uid_t uid;
	uid = from_kuid(targ, kuid);

    // 如果 uid 是 -1 的話,把 uid 設置成 65534 (overflow)
	if (uid == (uid_t) -1)
		uid = overflowuid;
	return uid;
}

from_kuid()

static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
{
	return __kuid_val(kuid);
}

__kuid_val()

static inline uid_t __kuid_val(kuid_t uid)
{
    // 獲取 kuid_t 結構的 val,這個就是 uid 的值了
	return uid.val;
}

geteuid

獲取用戶有效標識號

via:https://man7.org/linux/man-pages/man2/geteuid.2.html

returns the effective user ID of the calling process.

SYSCALL_DEFINE0(geteuid)
{
	/* Only we change this so SMP safe */
	return from_kuid_munged(current_user_ns(), current_euid());
}

其實和上面的 getuid 是一樣的

總結

獲取用戶標識號 和 獲取用戶有效標識號 其實就是獲取 current->cred->uid->val, current->cred->euid->val


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM