Linux 中的特殊文件权限:SUID、GUID 和 Sticky

news2024/9/24 11:25:23

注: 机翻,未校。


Special File Permissions in Linux: SUID, GUID and Sticky Bit

You see an s instead of x in the file permissions? Linux has some special file permissions called SUID, GUID and Sticky Bit. Know more about them.
在文件权限中看到 s 而不是 x?Linux 有一些特殊的文件权限,称为 SUID、GUID 和 Sticky Bit。

File permissions and ownership are the basic and yet essential security concepts in Linux. You probably are already familiar with these terms already. It typically looks like this:
文件权限和所有权是 Linux 中基本但必不可少的安全概念。可能已经熟悉这些术语。它通常看起来像这样:

在这里插入图片描述
Regular file permissions

Apart from these regular permissions, there are a few special file permissions and not many Linux users are aware of it.
除了这些常规权限外,还有一些特殊的文件权限,并且没有多少 Linux 用户知道它。
在这里插入图片描述
Linux Special Permissions: SUID, GUID and Sticky Bit

To start talking about of special permissions, I am going to presume that you have some knowledge of the basic file permissions. If not, please read our excellent guide explaining Linux file permission.
在开始讨论特殊权限之前,假设对基本文件权限有一定的了解。

Now I’m gonna show you some special permissions with new letters on the Linux file system.
现在,将展示 Linux 文件系统上带有新字母的一些特殊权限。

In this example, the passwd command, responsible to change the password of a user, has the letter s on the same place we expect to see x or -, for user permissions. It’s important to notice that this file belongs to the root user and root group.
在此示例中,负责更改用户密码的 passwd 命令在期望看到的 x- 的同一位置具有字母 s,用于用户权限。请务必注意,此文件属于 root 用户和 root 组。

With this permission, you don’t need to give sudo access to a specific user when you want him to run some root script.
通过这种权限设置,无需给予特定用户 sudo 访问权限,就可以让他运行一些以 root 身份执行的脚本。

What is SUID?

When the SUID bit is set on an executable file, this means that the file will be executed with the same permissions as the owner of the executable file.
当在可执行文件上设置 SUID 位时,这意味着将以与可执行文件所有者相同的权限执行该文件。

在这里插入图片描述

Let’s take a practical example. If you look at the binary executable file of the passwd command, it has the SUID bit set.
让举一个实际的例子。如果查看 passwd 命令的二进制可执行文件,它会设置了 SUID 位。

linux:~$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 59640 Mar 22 2019 /usr/bin/passwd

This means that any user running the passwd command will be running it with the same permission as root.
这意味着任何运行 passwd 命令的用户都将以与 root 相同的权限运行它。

What’s the benefit? The passwd command needs to edit files like /etc/passwd, /etc/shadow to change the password. These files are owned by root and can only be modified by root. But thanks to the setuid flag (SUID bit), a regular user will also be able to modify these files (that are owned by root) and change his/her password.
有什么好处?passwd 命令需要编辑 /etc/passwd、/etc/shadow 等文件以更改密码。这些文件归 root 所有,只能由 root 修改。但是多亏了 setuid 标志(SUID 位),普通用户也将能够修改这些文件(由 root 拥有)并更改他/她的密码。

This is the reason why you can use the passwd command to change your own password despite of the fact that the files this command modifies are owned by root.
这就是为什么可以使用 passwd 命令更改自己的密码的原因,尽管此命令修改的文件归 root 所有。

Why can a normal user not change the password of other users?

Note that a normal user can’t change passwords for other users, only for himself/herself. But why? If you can run the passwd command as a regular user with the same permissions as root and modify the files like /etc/passwd, why can you not change the password of other users?
请注意,普通用户不能为其他用户更改密码,只能为自己更改密码。但是为什么?如果可以以与root相同权限的普通用户身份运行passwd命令并修改/etc/passwd之类的文件,为什么不能更改其他用户的密码?

If you check the code for the passwd command, you’ll find that it checks the UID of the user whose password is being modified with the UID of the user that ran the command. If it doesn’t match and if the command wasn’t run by root, it throws an error.
如果检查 passwd 命令的代码,会发现它会检查密码正在修改的用户的 UID 与运行命令的用户的 UID 一起检查。如果它不匹配,并且如果命令不是由 root 运行的,则会引发错误。

The setuid/SUID concept is tricky and should be used with utmost cautious otherwise you’ll leave security gaps in your system. It’s an essential security concept and many commands (like ping command and programs (like sudo) utilize it.
setuid/SUID 概念很棘手,应非常谨慎地使用,否则会在系统中留下安全漏洞。这是一个基本的安全概念,许多命令(如 ping 命令)和程序(如 sudo)都使用它。

Now that you understand the concept SUID, let’s see how to set the SUID bit.
现在已经了解了 SUID 的概念,让看看如何设置 SUID 位。

How to set SUID bit?

I find the symbolic way easier while setting SUID bit. You can use the chmod command in this way:
发现在设置 SUID 位时,符号方式更容易。可以通过以下方式使用 chmod 命令:

chmod u+s file_name

Here’s an example: 下面是一个示例:

linux:~$ ls -l test.txt
-rwxrw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:51 test.txt
linux:~$ chmod u+s test.txt
linux:~$ ls -l test.txt
-rwsrw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:52 test.txt

You can also use the numeric way. You just need to add a fourth digit to the normal permissions. The octal number used to set SUID is always 4.
也可以使用数字方式。只需要在正常权限中添加第四位数字即可。用于设置 SUID 的八进制数始终为 4。

linux:~$ ls -l test2.txt
-rwxrw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:53 test2.txt
linux:~$ chmod 4766 test2.txt
linux:~$ ls -l test2.txt
-rwsrw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:54 test2.txt

How to remove SUID?

You can use either the symbolic mode in chmod command like this:
可以在 chmod 命令中使用符号模式,如下所示:

chmod u-s test.txt

Or, use the numeric way with 0 instead of 4 with the permissions you want to set:
或者,使用带有 0 而不是 4 的数字方式来设置要设置的权限:

chmod 0766 test2.txt
Difference between small s and capital S as SUID bit

Remember the definition of SUID? It allows a file to be executed with the same permissions as the owner of the file.
还记得SUID的定义吗?它允许使用与文件所有者相同的权限执行文件。

But what if the file doesn’t have execute bit set in the first place? Like this:
但是,如果文件一开始就没有设置执行位怎么办?喜欢这个:

linux:~$ ls -l test.txt
-rw-rw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:51 test.txt

If you set the SUID bit, it will show a capital S, not small s:
如果设置 SUID 位,它将显示大写字母 S,而不是小 s:

linux:~$ chmod u+s test.txt
linux:~$ ls -l test.txt
-rwSrw-rw- 1 linuxhandbook linuxhandbook 0 Apr 12 17:52 test.txt

The S as SUID flag means there is an error that you should look into. You want the file to be executed with the same permission as the owner but there is no executable permission on the file. Which means that not even the owner is allowed to execute the file and if file cannot be executed, you won’t get the permission as the owner. This fails the entire point of setting the SUID bit.
S as SUID 标志表示存在应调查的错误。希望以与所有者相同的权限执行文件,但该文件没有可执行权限。这意味着甚至连所有者都不允许执行该文件,如果文件无法执行,将无法获得作为所有者的权限。这无法满足设置 SUID 位的全部意义。

How to find all files with SUID set?

If you want to search files with this permission, use find command in the terminal with option -perm.
如果要使用此权限搜索文件,请在终端中使用带有 -perm 选项的 find 命令。

find / -perm /4000

What is SGID?

SGID is similar to SUID. With the SGID bit set, any user executing the file will have same permissions as the group owner of the file.
SGID类似于SUID。设置了 SGID 位后,执行文件的任何用户都将具有与文件组所有者相同的权限。

It’s benefit is in handling the directory. When SGID permission is applied to a directory, all sub directories and files created inside this directory will get the same group ownership as main directory (not the group ownership of the user that created the files and directories).
它的好处是处理目录。当将 SGID 权限应用于目录时,在此目录中创建的所有子目录和文件将获得与主目录相同的组所有权(而不是创建文件和目录的用户的组所有权)。

在这里插入图片描述

Open your terminal and check the permission on the file /var/local:
打开终端并检查文件 /var/local 的权限:

linux:~$ ls -ld /var/local
drwxrwsr-x 1 root staff 512 Apr 24 2018 /var/local

This folder /var/local has the letter s on the same place you expect to see x or - for group permissions.
此文件夹 /var/local 的同一位置包含字母“s”,希望看到组权限的“x”或“-”。

A practical example of SGID is with Samba server for sharing files on your local network. It’s guaranteed that all new files will not lose the permissions desired, no matter who created it.
SGID的一个实际例子是与Samba服务器一起使用,用于在本地网络上共享文件。可以保证所有新文件都不会丢失所需的权限,无论它是由谁创建的。

How to set SGID?

You can set the SGID bit in symbolic mode like this:
可以像这样在符号模式下设置 SGID 位:

chmod g+s directory_name

Here’s an example: 下面是一个示例:

linux:~$ ls -ld folder/
drwxrwxr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:32 folder/
linux:~$ chmod g+s folder
linux:~$ ls -ld folder/
drwxrwsr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:32 folder/

You may also use the numeric way. You just need to add a fourth digit to the normal permissions. The octal number used to SGID is always 2.
也可以使用数字方式。只需要在正常权限中添加第四位数字即可。用于 SGID 的八进制数始终为 2。

linux:~$ ls -ld folder2/
drwxrwxr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:33 folder2/
linux:~$ chmod 2775 folder2
linux:~$ ls -ld folder2/
drwxrwsr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:33 folder2/

How to remove SGID bit?

Just use the -s instead of +s like this:
只需使用 -s 而不是 +s,如下所示:

chmod g-s folder

Removing SGID is the same as removing SGID. Use the additional 0 before the permissions you want to set:
删除 SGID 与删除 SGID 相同。在要设置的权限之前使用额外的 0:

chmod 0755 folder

How to find files with SGID set in Linux

To find all the files with SGID bit set, use this command:
要查找设置了 SGID 位的所有文件,请使用以下命令:

find . -perm /2000

What is a Sticky Bit?

The sticky bit works on the directory. With sticky bit set on a directory, all the files in the directory can only be deleted or renamed by the file owners only or the root.
粘滞位在目录上工作。在目录上设置粘滞位时,目录中的所有文件只能由文件所有者或根用户删除或重命名。

在这里插入图片描述

This is typically used in the /tmp directory that works as the trash can of temporary files.
这通常在 /tmp 目录中使用,该目录充当临时文件的垃圾桶。

linux:~$ ls -ld /tmp
drwxrwxrwt 1 root root 512 Apr 12 13:24 /tmp

As you can see, the folder /tmp, has the letter t on the same place we expect to see x or for others permissions. This means that a user (except root) cannot delete the temporary files created by other users in the /tmp directory.
如所见,文件夹 /tmp 在希望看到 x 或 – 的同一位置上有字母 t,用于其他权限。这意味着用户(root 除外)无法删除其他用户在 /tmp 目录中创建的临时文件。

How to set the sticky bit? 如何设置粘滞位?

As always, you can use both symbolic and numeric mode to set the sticky bit in Linux.
与往常一样,可以在 Linux 中使用符号和数字模式来设置粘滞位。

chmod +t my_dir

Here’s an example: 下面是一个示例:

linux:~$ ls -ld my_dir/
drwxrwxr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:54 my_dir/
linux:~$ chmod +t my_dir/
linux:~$ ls -ld my_dir/
drwxrwxr-t 2 linuxhandbook linuxhandbook 4096 Apr 12 19:54 my_dir/

The numeric way is to add a fourth digit to the normal permissions. The octal number used for sticky bit is always 1.
数字方式是在正常权限上添加第四位数字。用于粘滞位的八进制数始终为 1。

linux:~$ ls -ld my_dir/
drwxrwxr-x 2 linuxhandbook linuxhandbook 4096 Apr 12 19:55 my_dir/
linux:~$ chmod 1775 tmp2/
linux:~$ ls -ld tmp2/
drwxrwxr-t 2 linuxhandbook linuxhandbook 4096 Apr 12 19:55 my_dir/

How to remove the sticky bit: 如何去除粘性钻头:

You can use the symbolic mode:
可以使用符号模式:

chmod -t my_dir

Or the numeric mode with 0 before the regular permissions:
或者常规权限前加 0 的数字模式:

chmod 0775 tmp2

How to find files with sticky bit set in Linux 如何在 Linux 中查找设置了粘滞位的文件

This command will return all files/directories in with sticky bit set:
此命令将返回设置了粘滞位的所有文件/目录:

linux:~$ find . -perm /1000

If the directory doesn’t have the execute permission set for all, setting a sticky bit will result in showing T instead of t. An indication that things are not entirely correct with the sticky bit.
如果目录没有为所有目录设置执行权限,则设置粘滞位将导致显示 T 而不是 t。表明粘性位的情况并不完全正确。

Conclusion 结论

I’ll put this picture here to recall what you have just learned:
把这张图片放在这里,是为了回忆你刚刚学到的东西:

在这里插入图片描述

This flexibility to manage folders, files and all their permissions are so important in the daily work of a sysadmin. You could see that all those special permissions are not so difficult to understand but they must be used with utmost caution.
这种管理文件夹、文件及其所有权限的灵活性在系统管理员的日常工作中非常重要。可以看到,所有这些特殊权限并不难理解,但必须非常谨慎地使用它们。


via:

  • What is SUID, GUID and Sticky Bit in Linux? How to Use Them? Sep 15, 2022 — Abhishek Prakash

    https://linuxhandbook.com/suid-sgid-sticky-bit/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1981829.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

了解MVCC

概念 MVCC,全称Multi-Version Concurrency Control,即多版本并发控制,是一种并发控制的方法,维护一个数据的多个版本,使得读写操作没有冲突,快照读为MySQL实现MVCC提供了一个非阻塞读功能。MVCC的具体实现…

C++新手入门学习教程(完整版)

以下教程覆盖了 C 学习的各个方面,适合初学者循序渐进地学习。学习过程中,建议初学者多做练习和项目,以加深对理论知识的理解。希望这个教程能为你提供一个清晰的学习路径。 目录 第一章:C 简介 1.1 C 的历史与演变 1.2 C 的特…

低代码: 系统开发准备之确定一般开发流程,需求分析,技术架构

概述 低代码系统开发之前,我们首先要进行一些准备我们首先知道我们软件开发的一般流程同时,我们还要知道,我们整个系统平台的需求如何之后,我们要基于需求进行设计,包含UI设计与系统架构设计 一般开发流程 系统开发…

数据库典型例题1-画ER图

1.question: solution: 2.画图步骤&#xff1a; 1.圈实体 2.写出实体间关系&#xff0c;确定实体画图位置 3.圈出每个实体的属性 4.画图&#xff0c;注意特殊实体和属性 3.注意点 <1 弱实体 弱实体&#xff1a;一个实体必须依赖于另一个实体存在---->实体双线菱形弱…

centos Python3.6升级3.8

CentOS系统上升级Python3.6到3.8版本。 步骤 1. 更新系统 在开始升级Python之前&#xff0c;首先需要确保系统是最新的。可以使用以下命令更新CentOS系统&#xff1a; sudo yum update 2. 安装依赖项 升级Python之前&#xff0c;需要安装一些依赖项。运行以下命令安装这些依赖…

Common Lisp精解【1】

文章目录 概述什么是 Common Lisp概念历史 应用领域1. 人工智能2. 自然语言处理3. 数据分析4. 系统编程5. Web开发6. 教育与研究7. 其他领域 基础 参考文献 概述 以下内容来自文心一言的自动生成 什么是 Common Lisp Common Lisp&#xff08;缩写为CL&#xff09;是Lisp编程语…

Unity使用Modbus协议

最近一直在工业领域干活&#xff0c;学习下Modbus协议&#xff0c;这里做个记录&#xff0c;理解不对的地方希望大佬指出修正。 一、先上测试工具和Unity脚本。 1.测试工具使用的 Modsim32 2.Unity测试脚本如下 /* 0x01&#xff1a;读线圈 0x05&#xff1a;写单个线圈 0x0F…

计算机组成原理 —— 指令流水线影响因素分类

计算机组成原理 —— 指令流水线影响因素分类 结构冒险结构冒险的原因 数据冒险&#xff08;同步&#xff09;数据旁路的原理数据旁路的类型数据旁路的例子 控制冒险控制冒险的类型控制冒险的例子解决控制冒险的方法示例分析分支预测的策略 超标量和超流水超标量&#xff08;Su…

关于计算机的思考

本文是《Python入门经典以解决计算问题为导向的Python编程实践》一书中第一部分“关于计算机的思考”的笔记&#xff0c;后附上思维导图。 关于计算机的思考 一、为什么要研究计算机科学1、重要性2、“计算机科学”更强调计算而不是编程3、从”通过编程解决问题“的思路出发&a…

RPC通信的简单流程

远程调用者假设需要调用Login方法&#xff0c;将调用的信息通过muduo库&#xff0c;同时进行了序列化和反序列化&#xff0c;发送到Rpcprovider上&#xff0c;RpcProvider通过对象和方法表来确定需要调用哪个服务对象的哪个方法。 UserRpcServiceRpc和UseRpcServiceRpcStub是继…

去中心化社交:探讨Facebook在区块链平台上的实践

随着区块链技术的崛起&#xff0c;社交平台也面临着前所未有的变革。作为全球最大的社交平台之一&#xff0c;Facebook&#xff08;现名Meta Platforms&#xff09;正在积极探索如何将区块链技术融入其平台&#xff0c;以引领去中心化社交的新时代。本文将从不同角度探讨Facebo…

leaflet.motion台风路径动画绘制

在气象领域中&#xff0c;对台风的准确可视化呈现对于灾害预警和防范具有重要意义。本文将深入探讨一段使用 JavaScript 实现台风可视化功能的代码。原本只是简单的绘制台风的路径&#xff0c;但是后面的需求要求显示台风各个历史节点的动画绘制&#xff0c;于是难度增加了&…

《安全历史第4讲——从古代驿站体系看软件安全管控》

在古代&#xff0c;车、马都很慢&#xff0c;信息传递很不顺畅&#xff0c;中央的政令又是如何传达至地方的呢&#xff1f;实际上&#xff0c;很多朝代都有专门的驿站制度&#xff0c;可以保障全国各地的信息传递&#xff0c;对于维护统治和稳定有着关键作用。 若将国家比作一个…

OZON刚需产品哪些好卖,OZON刚需热卖产品

OZON平台上的刚需热卖产品涵盖了多个领域&#xff0c;这些产品通常能够满足消费者的基本需求或提升生活品质。以下是一些在OZON平台上表现良好的刚需热卖产品类别及其特点&#xff01; OZON刚需热卖产品地址&#xff1a;D。DDqbt。COm/74rDTop1 防蚊修复网 Скотч сет…

二次注入(2018网鼎杯comment)

一、2018网鼎杯comment 该题主要考察二次注入 1.二次注入概念&#xff1a; 攻击者构造恶意的数据并存储在数据库后&#xff0c;恶意数据被读取并进入到SQL查询语句所导致的注入。防御者可能在用户输入恶意数据时对其中的特殊字符进行了转义处理&#xff0c;但在恶意数据插入…

【LeetCode Cookbook(C++ 描述)】一刷二叉树之递归遍历(DFS)(上)

目录 二叉树的实现LeetCode #144&#xff1a;Binary Tree Preorder Traversal 二叉树的前序遍历递归解法「遍历」思路「分而治之」思路更多例子&#xff1a;求二叉树最大深度 迭代解法Morris 遍历 LeetCode #94&#xff1a;Binary Tree Inorder Traversal 二叉树的中序遍历迭代…

亲民且友好的Top期刊,最快46天录用!

本期小编解析一本超亲民超友好的Top期刊&#xff0c;发文量大&#xff0c;编辑处理速度极快&#xff08;近期案例46天录用&#xff09;&#xff0c;毕业有高分区发文要求的小伙伴&#xff0c;赶紧码住这一本神刊&#xff01; 期刊简介 Knowledge-Based Systems (KBS) 出版社 …

AI智能测评应用平台项目分享

大家好&#xff0c;我是程序媛雪儿&#xff0c;今天咱们聊个我新学的项目&#xff0c;AI智能评测应用平台系统。 咱们先了解一下这个系统是干嘛的。 一、业务分析 大致业务流程是应用制作者在创建应用页面填写应用信息&#xff0c;依次添加题目和评分规则生成测评应用&#xff…

信息学奥赛初赛天天练-56-CSP-J2019完善程序2-双关键字排序、计数排序、前缀和、前缀自增、后缀自增、数组下标自增

PDF文档公众号回复关键字:20240805 1 完善程序 (单选题 &#xff0c;每小题3分&#xff0c;共30分) 计数排序 计数排序是一个广泛使用的排序方法。下面的程序使用双关键字计数排序&#xff0c;将 n 对 10000以内的整数&#xff0c;从小到大排序。 例如有三对整数 (3,4)、(2,…

标准IO及相关练习

标准IO 能够将指定的数据写入指定的文件中的操作&#xff08;通过文件指针去访问指定的文件&#xff1a;FILE*&#xff09;&#xff0c;标注IO只提供写入或者读取操作&#xff0c;不提供删除文件中的内容&#xff0c;想要删除文件&#xff0c;则需要自己写逻辑来实现。 文件指…