SUID对Shell脚本无效

13

我已经创建了一个包含以下内容的小型shell脚本:

cat /usr/bin/checksuid.sh

!/bin/bash
echo "Hello" > /etc/myfile.cnf

ls -l /usr/bin/checksuid.sh
-rwsr-xr-x 1 root root 56 Sep  9 12:56 /usr/bin/checksuid.sh

我还创建了一个名为/etc/myfile.cnf的文件,并使用root账户设置了以下权限:

/etc/myfile.cnf是中央配置文件,用于指定应用程序的特定配置选项。

注意:确保只有具有必要访问权限的用户才能读取或修改此文件。

请谨慎处理该文件,以免对系统造成不良影响。

-rw-r--r-- 1 root root 6 Sep  9 12:26 /etc/myfile.cnf

当我在非root账户下执行/usr/bin/checksuid.sh时,我会得到以下错误:

/usr/bin/checksuid.sh: line 3: /etc/myfile.cnf: Permission denied

有人能帮助你解决SUID无法正常工作的问题吗?


它也应该是#!/bin/bash作为Bash的shebang行,但最好是#!/bin/sh - Jite
2个回答

23

16

来自http://www.tuxation.com/setuid-on-shell-scripts.html:

"实际上,因为会带来严重的安全漏洞,所以很多*nix系统实现都禁用了setuid位"

另一种方法是将脚本包装在可以使用setuid的东西中,就像这个示例C程序。显然,与直接调用脚本相比,使用这样的包装程序存在某些差异(例如忽略了退出代码),但这应该能给您一个想法。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   setuid( 0 );
   system( "/path/to/script.sh" );

   return 0;
}

4
在调用脚本之前/时,应清除环境变量以降低安全风险。 - Meixner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接