Linux內核進程管理子系統有什么第四十六回 —— 進程主結構詳解(42)

接前一篇文章:Linux內核進程管理子系統有什么第四十五回 —— 進程主結構詳解(41)

?

本文內容參考:

Linux內核進程管理專題報告_linux rseq-CSDN博客

《趣談Linux操作系統 核心原理篇:第三部分 進程管理》—— 劉超

《圖解Linux內核?基于6.x》 —— 姜亞華 機械工業出版社

setuid系統調用及示例-CSDN博客

setuid函數解析 - HelloMarsMan - 博客園

特此致謝!

?

進程管理核心結構 —— task_struct

8. 進程權限相關成員

進程權限相關成員包括以下幾個:

??	/* Process credentials: *//* Tracer's credentials at attach: */const struct cred __rcu		*ptracer_cred;/* Objective and real subjective task credentials (COW): */const struct cred __rcu		*real_cred;/* Effective (overridable) subjective task credentials (COW): */const struct cred __rcu		*cred;

這幾個字段的描述如下:

?

上一回繼續對于struct cred進行解析,本回仍然繼續。為了便于理解和回顧,再次貼出struct cred的定義,在include/linux/cred.h中,如下:

/** The security context of a task** The parts of the context break down into two categories:**  (1) The objective context of a task.  These parts are used when some other*	task is attempting to affect this one.**  (2) The subjective context.  These details are used when the task is acting*	upon another object, be that a file, a task, a key or whatever.** Note that some members of this structure belong to both categories - the* LSM security pointer for instance.** A task has two security pointers.  task->real_cred points to the objective* context that defines that task's actual details.  The objective part of this* context is used whenever that task is acted upon.** task->cred points to the subjective context that defines the details of how* that task is going to act upon another object.  This may be overridden* temporarily to point to another security context, but normally points to the* same context as task->real_cred.*/
struct cred {atomic_t	usage;
#ifdef CONFIG_DEBUG_CREDENTIALSatomic_t	subscribers;	/* number of processes subscribed */void		*put_addr;unsigned	magic;
#define CRED_MAGIC	0x43736564
#define CRED_MAGIC_DEAD	0x44656144
#endifkuid_t		uid;		/* real UID of the task */kgid_t		gid;		/* real GID of the task */kuid_t		suid;		/* saved UID of the task */kgid_t		sgid;		/* saved GID of the task */kuid_t		euid;		/* effective UID of the task */kgid_t		egid;		/* effective GID of the task */kuid_t		fsuid;		/* UID for VFS ops */kgid_t		fsgid;		/* GID for VFS ops */unsigned	securebits;	/* SUID-less security management */kernel_cap_t	cap_inheritable; /* caps our children can inherit */kernel_cap_t	cap_permitted;	/* caps we're permitted */kernel_cap_t	cap_effective;	/* caps we can actually use */kernel_cap_t	cap_bset;	/* capability bounding set */kernel_cap_t	cap_ambient;	/* Ambient capability set */
#ifdef CONFIG_KEYSunsigned char	jit_keyring;	/* default keyring to attach requested* keys to */struct key	*session_keyring; /* keyring inherited over fork */struct key	*process_keyring; /* keyring private to this process */struct key	*thread_keyring; /* keyring private to this thread */struct key	*request_key_auth; /* assumed request_key authority */
#endif
#ifdef CONFIG_SECURITYvoid		*security;	/* LSM security */
#endifstruct user_struct *user;	/* real user ID subscription */struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */struct ucounts *ucounts;struct group_info *group_info;	/* supplementary groups for euid/fsgid *//* RCU deletion */union {int non_rcu;			/* Can we skip RCU deletion? */struct rcu_head	rcu;		/* RCU deletion hook */};
} __randomize_layout;

前邊幾回結合例程,講解setuid在不同權限下的行為。分別講解了:

(1)調用者是特權用戶(root)的情況

(2)調用者是普通用戶(程序文件擁有者)的情況

(3)調用者是普通用戶(非程序文件擁有者)的情況。

上一回筆者說雖然setuid系統調用的各種情況都講了,但并未講到真正的精髓。本回就來講講這個精髓之處。為了便于理解和回顧,再次貼出例程代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>void show_curr_uids(void)
{int ret;uid_t real_uid;uid_t effective_uid;uid_t saved_uid;ret = getresuid(&real_uid, &effective_uid, &saved_uid);printf("Real uid: %d, Effective uid: %d\n", real_uid, effective_uid);printf("Current UIDs:\n");printf("\tReal uid: %d\n\tEffective uid: %d\n\tSaved uid: %d\n", getuid(), geteuid(), (getresuid(&real_uid, &effective_uid, &saved_uid) == 0) ? saved_uid : -1);
}int main(int argc, char *argv[])
{int ret;uid_t original_ruid, original_euid, original_suid;uid_t target_uid;//獲取并打印初始 UIDif (getresuid(&original_ruid, &original_euid, &original_suid) != 0){perror("getresuid");exit(EXIT_FAILURE);}//檢查是否以root權限運行if (geteuid() == 0){printf("\nRunning as ROOT (Privileged User)\n");printf("Before setuid.\n");show_curr_uids();//作為root,可以設置為任意有效的UID//這里我們嘗試設置為'nobody' 用戶 (通常UID 65534, 但請檢查你的系統)target_uid = 65534;printf("\nAttempting to set UID to %d (usually 'nobody' user)...\n", target_uid);ret = setuid(target_uid);if (ret == 0){printf("setuid(%d) succeeded.\n", target_uid);printf("\nAfter setuid.\n");show_curr_uids();printf("Note: All UIDs (Real, Effective, Saved) are now %d.\n", target_uid);printf("The process is now running with 'nobody' privileges.\n");}else{perror("setuid");printf("Failed to set UID to %d.\n", target_uid);}}else{printf("\nRunning as a REGULAR USER (UID: %d)\n", getuid());printf("Before setuid.\n");show_curr_uids();//作為普通用戶,只能設置為自己的ruid或suidtarget_uid = original_ruid; //選擇設置為自己的真實UID (這不會改變任何東西)printf("\nAttempting to set UID to my Real UID (%d)...\n", target_uid);ret = setuid(target_uid);if (ret == 0){printf("setuid(%d) succeeded (as expected).\n", target_uid);printf("\nAfter setuid.\n");show_curr_uids();}else{perror("setuid");printf("Failed to set UID to %d.\n", target_uid);}#if 0//嘗試設置為一個無效的UID(比如一個不存在的或不屬于我的UID)//這通常會失敗target_uid = 9999; //假設這是一個無效的或不屬于當前用戶的UIDprintf("\nAttempting to set UID to an invalid/different UID (%d)...\n", target_uid);ret = setuid(target_uid);if (ret == -1){if (errno == EPERM){printf("setuid(%d) failed with EPERM (Operation not permitted) - as expected for a regular user.\n", target_uid);printf("This is because %d is not my Real UID (%d) or Saved Set-UID (%d).\n", target_uid, original_ruid, original_suid);}else{perror("setuid");printf("Failed to set UID to %d.\n", target_uid);}printf("After failed setuid.\n");show_curr_uids();}else{printf("setuid(%d) unexpectedly succeeded.\n", target_uid);printf("After unexpected setuid.\n");show_curr_uids();}
#endif}return 0;
}

這次只關注代碼中else分支的前半段,也就是:

	//檢查是否以root權限運行if (geteuid() == 0){……}else{printf("\nRunning as a REGULAR USER (UID: %d)\n", getuid());printf("Before setuid.\n");show_curr_uids();//作為普通用戶,只能設置為自己的ruid或suidtarget_uid = original_ruid; //選擇設置為自己的真實UID (這不會改變任何東西)printf("\nAttempting to set UID to my Real UID (%d)...\n", target_uid);ret = setuid(target_uid);if (ret == 0){printf("setuid(%d) succeeded (as expected).\n", target_uid);printf("\nAfter setuid.\n");show_curr_uids();}else{perror("setuid");printf("Failed to set UID to %d.\n", target_uid);}#if 0……
#endif}

這次要做的實驗步驟如下:

1)首先,通過chmod u+s /tmp/setuid_example命令,對setuid_example添加上SUID權限

實際命令及結果如下:

?

2)使用shiyan用戶(UID為1001)來運行setuid_example程序

這個程序的實際用戶ID(real UID)為1001,而有效用戶ID(effective UID)和保存的設置用戶ID(saved UID)都為1000。接著,在這個程序中調用setuid函數,傳入的參數為1001,大家可以猜想到,最終應該只有有效用戶ID受到影響(會從1000變為1001),而保存的設置用戶ID保持不變。

實測結果如下:

?

實測結果與預期一致。

?

?

再來對比一下沒有使用chmod u+s /tmp/setuid_example命令之前的結果:

?

也對比一下示例代碼中將target_uid(uid)設置為1000(用戶ph的UID)的結果:

?

雖然上面的示例在運行時調用setuid,但setuid系統調用的強大之處還體現在可執行文件的SUID位上。這就要和chmod u+s命令結合起來,共同起作用了。

當將一個可執行文件的SUID位設置為1時(如通過chmod u+s xxx),會發生以下情況:

無論哪個用戶執行這個文件,該進程啟動時的有效用戶ID(EUID)均會被設置為該文件所有者的用戶ID。這使得普通用戶可以運行一個具有程序文件所有者權限的程序。

這一點從上邊的結果就可以看出來,沒有執行chmod u+s /tmp/setuid_example之前,Effective UID是1001,也就是用戶shiyan;

執行chmod u+s /tmp/setuid_example后,Effective UID是1000,也就是setuid_example的屬主ph了。

為了更好地理解setuid和chmod u+s,這里再給出一個例子:

至此,struct cred的前4組字段就解析完了。下一回繼續解析后續字段。

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/921653.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/921653.shtml
英文地址,請注明出處:http://en.pswp.cn/news/921653.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux網絡連接不上?NetworkManager提示“device not managed“!

#操作系統 #Linux #NetworkManager適用環境kylin v10Centos 8Redhat 8一、故障現象在CentOS/RHEL(同樣適用于kylin v10&#xff09;系統中&#xff0c;管理員執行 nmcli connection up ens160 命令嘗試激活名為 ens160 的網絡連接時&#xff0c;遇到以下錯誤&#xff1a;[roo…

【系統分析師】第2章-基礎知識:數學與工程基礎(核心總結)

更多內容請見: 備考系統分析師-專欄介紹和目錄 文章目錄 一、數學統計基礎 1.1 概率論基礎 1.2 數理統計基礎 1.3 常用統計分析方法 二、圖論應用 2.1 基本概念 2.2 核心算法與應用 三、預測與決策 3.1 預測方法 3.2 決策方法 四、數學建模 4.1 建模過程 4.2 常用模型類型 五、…

StrUtil.isBlank()

這段代碼是一個條件判斷&#xff0c;用于檢查變量 shopJson 是否為空或空白&#xff0c;如果是&#xff0c;就直接返回 null。我們來逐句講解&#xff1a;原始代碼&#xff1a; if(StrUtil.isBlank(shopJson)) {// 3.存在&#xff0c;直接返回return null; }逐句解釋&#xff1…

mysql 回表查詢(二次查詢,如何檢查,如何規避)

h5打開以查看 “回表查詢”通常發生在使用二級索引&#xff08;Secondary Index&#xff09;的查詢中。當查詢所需的數據列并不全部包含在二級索引中時&#xff0c;即使使用了索引&#xff0c;MySQL 也需要根據索引記錄中的主鍵值&#xff0c;回到聚簇索引&#xff08;Cluster…

深度學習(二):神經元與神經網絡

在人工智能的浪潮中&#xff0c;神經網絡&#xff08;Neural Networks&#xff09;無疑是驅動核心技術的引擎&#xff0c;它賦予了計算機前所未有的學習和識別能力。而這一切的起點&#xff0c;是受到生物大腦中基本單元——神經元&#xff08;Neurons&#xff09;的深刻啟發。…

JavaScript 行為型設計模式詳解

1. 觀察者模式1.1. 使用場景觀察者模式用于對象間的一對多依賴關系&#xff0c;當一個對象的狀態發生變化時&#xff0c;所有依賴于它的對象都能收到通知并自動更新。常用于事件處理、通知系統。在前端中&#xff0c;觀察者模式用于實現事件監聽、數據綁定等功能。1.2. 代碼實現…

指令查找表LUT

本文整理自22. FlexSPI—讀寫外部SPI NorFlash — [野火]i.MX RT庫開發實戰指南——基于i.MXRT1052 文檔 用作個人學習和分享 指令查找表LUT 訪問FLASH存儲器通常包含一些讀寫功能的的控制指令&#xff0c;主控設備可通過這些指令訪問FLASH存儲器。 為了適應這種需求&#…

uv使用指南

&#x1f680; Python 打包工具 UV 使用指南 UV 是一個用 Rust 編寫的極速 Python 包管理器和解析器&#xff0c;旨在成為 pip、pip-tools、virtualenv 等工具的單一替代方案。 &#x1f4cb; 目錄 核心概念與設計哲學安裝與配置基礎使用方法項目管理與工作流高級功能與技巧…

安卓學習 之 圖片控件和圖片按鈕

今天學習的是ImageView 和 ImageButton這兩個控件還是比較簡單的&#xff1a;先來看看最后的樣式圖片吧&#xff1a;從圖片中可以看到ImageView中的圖片要大很多&#xff0c;這是因為中的ImageView中的圖片跟ImageView控件的大小而自動調整。Imag…

動態規劃-學習筆記

這是一份動態規劃&#xff08;Dynamic Programming, DP&#xff09;完整學習筆記。筆記將從一星難度&#xff08;入門&#xff09;到五星難度&#xff08;進階&#xff09;&#xff0c;循序漸進&#xff0c;涵蓋核心思想、經典模型和解題方法論。 本來打算今天更新背包問題的題…

Linux 可信啟動深度解析:從UEFI到操作系統的信任鏈

文章目錄引言一、 可信根基&#xff1a;TPM與核心概念1.1 什么是“度量” (Measurement)&#xff1f;1.2 信任鏈與TPM PCR二、 階段一&#xff1a;固件的可信啟動 (UEFI)2.1 引導的起點&#xff1a;從SEC到DXE的初始化2.2 引導設備選擇 (BDS)&#xff1a;UEFI如何找到GRUB2.3 S…

61-python中面向對象三大特性

前言&#xff1a; 面向對象編程&#xff0c;是許多編程語言都支持的一種編程思想。簡單理解是&#xff1a;基于模板&#xff08;類&#xff09;去創建實體&#xff08;對象&#xff09;&#xff0c; 使用對象完成功能開發。面向對象包含三大主要特性&#xff1a; 封裝 繼承 多態…

BP-Adaboost模型

BP-Adaboost模型是一種將BP神經網絡作為弱分類器的集成學習框架&#xff0c;通過AdaBoost算法動態調整樣本權重和模型權重&#xff0c;顯著提升預測精度和泛化能力。一、模型架構與工作原理 1. 基礎框架 弱分類器單元&#xff1a;采用單隱藏層BP神經網絡&#xff08;結構示例&a…

k230 +canMV+ LVGL控件 仿手表表盤觸摸屏滾動、選中后彈窗效果完整示例程序

現在智能手表用的越來越多,其交互方式比較有特點,現在k230開發板上,基于LVGL(Light and Versatile Graphics Library)編寫一個嵌入式GUI應用程序,使用LVGL配合觸摸屏實現模仿智能手表的表盤滾動效果,實際效果如下: 程序使用LVGL圖形庫和MediaManager程序,創建帶有觸摸…

使用Vue.js和WebSocket打造實時庫存儀表盤

大家好&#xff01;今天我將分享一個簡單卻強大的實時庫存儀表盤項目&#xff0c;基于Vue.js和WebSocket技術。這個項目適合初學者學習前端實時數據處理&#xff0c;也能為你的技術博客或作品集增添亮點&#xff01;通過這個教程&#xff0c;你將學會如何使用WebSocket實現實時…

leecode100——接雨水

題目 雙指針 思路1 使用參數存儲從左往右&#xff08;從右往左同理&#xff09;遍歷時的最高的柱子&#xff0c; 然后移動左右的指針&#xff0c;每次移動左右指針中偏向小的&#xff0c; 如果當前指針指的柱子小于最高的柱子&#xff0c;就會存在接到水。 思路2 把水看作柱子&…

復古膠片風格街拍人像Lr調色教程,手機濾鏡PS+Lightroom預設下載!

調色教程復古膠片風格街拍人像 Lightroom 調色&#xff0c;通過模擬經典膠片相機的色彩科學&#xff0c;為現代數碼照片注入懷舊韻味。這種調色手法注重低飽和度色彩、柔和的高光過渡和豐富的暗部細節&#xff0c;配合適度的顆粒感&#xff0c;營造出時光沉淀的質感。特別適合街…

Linux的gpio子系統

GPIO其實也是某個pin的功能之一。上一小節講解了 pinctrl 子系統&#xff0c;pinctrl 子系統重點是設置 PIN(有的 SOC 叫做 PAD)的復用和電氣屬性&#xff0c;如果 pinctrl 子系統將一個 PIN 復用為 GPIO 的話&#xff0c;那么接下來就要用到 gpio 子系統了。gpio 子系統顧名思…

VC++ CPU指令集檢測工具實現原理

&#x1f4c8; VC CPU指令集檢測工具實現原理 例圖&#xff1a;&#x1f9e0; 1. 核心原理&#xff1a;CPUID指令 // 使用CPUID指令獲取CPU信息 int cpuInfo[4] { -1 }; __cpuid(cpuInfo, 0); // 調用CPUID指令 int nIds cpuInfo[0]; // 獲取最大標準功能號CPUID指令工作流程…

大模型微調理論、實戰:LLaMA-Factory、Unsloth

概述 微調&#xff0c;Fine-Tuning&#xff0c;簡稱FT&#xff0c;可理解為對LLM的定制&#xff0c;目的是增強專業領域知識&#xff0c;并優化特定任務的性能。通過在特定數據集上微調一個預訓練模型&#xff0c;可實現&#xff1a; 更新知識&#xff1a;引入新的領域專屬信…