【问题标题】:SUID not working with shell scriptSUID 不适用于 shell 脚本
【发布时间】:2013-09-12 23:39:45
【问题描述】:

我创建了一个小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

我还使用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

标签: linux bash shell suid


【解决方案1】:

【讨论】:

    【解决方案2】:

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

    “事实上,setuid 位在许多 *nix 实现中被禁用,因为它会引发大量安全漏洞”

    另一种方法 - 将脚本包装在可以使用 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;
    }
    

    【讨论】:

    • 您应该在调用脚本之前/调用脚本时清除环境变量以降低安全风险。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2015-11-13
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多