简体   繁体   English

以非root用户身份调用外部shell脚本失败

[英]unsuccessfully call external shell script as non-root user

I want A user to do some jobs as B user. 我希望A用户做为B用户的一些工作。 It's OK if B is root, but non-root user failed. 如果B是root用户,则可以,但非root用户失败。 Here are basic codes: 以下是基本代码:

root.c: root.c:

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

int main()
{
    setuid( 0 );
    system( "/tmp/script_of_root.sh" );

    return 0;
}

script_of_root.sh: script_of_root.sh:

#!/bin/sh
echo $RANDOM >> /tmp/file_of_root

playback : 播放

$ cd /tmp
$ cc root.c -o root
$ su -
# chown root.root /tmp/root
# chmod 4755 /tmp/root
# exit
$ ./root

After executing "./root", file "/tmp/file_of_root" will be updated. 执行“ ./root”后,文件“ / tmp / file_of_root”将被更新。 But if I apply the same thing to a non-root user, it doesn't work. 但是,如果我将相同的内容应用于非root用户,则无法使用。 Codes: 代码:

foobar.c: foob​​ar.c但是:

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

int main()
{
    setuid( 1001 );    // uid of user "foobar"
    system( "/tmp/script_of_foobar.sh" );

    return 0;
}

script_of_foobar.sh: script_of_foobar.sh:

#!/bin/sh
echo $RANDOM >> /tmp/file_of_foobar

playback : 播放

$ cd /tmp
$ cc foobar.c -o foobar
$ su -
# chown foobar.users /tmp/foobar
# chmod 4755 /tmp/foobar
# exit
$ ./foobar

If I run "./foobar" as other normal user(not "foobar" itself), it's gonna be error: 如果我以其他普通用户身份(而不是“ foobar”本身)运行“ ./foobar”,那将是错误的:

/tmp/script_of_foobar.sh: line 2: file_of_foobar: Permission denied

I am totally confused. 我很困惑。 Why the second scenario not working? 为什么第二种情况不起作用?

Best regards. 最好的祝福。

The setuid call in foobar.c will only succeed if you are root, or your effective UID is 1001. 仅当您是root用户或有效UID为1001时,foobar.c中的setuid调用才会成功。

So. 所以。 If you're not root, setuid(1001) will fail, and you won't have the required rights to overwrite the file owned by "foobar". 如果您不是root用户,则setuid(1001)将失败,并且您将没有必需的权限来覆盖“ foobar”拥有的文件。

From the output it looks like your non-root user is able to execute script_of_foobar.sh, but unable to write to /tmp/file_of_foobar. 从输出看来,您的非root用户可以执行script_of_foobar.sh,但无法写入/ tmp / file_of_foobar。 Are the permissions on /tmp possibly off? / tmp的权限是否可能关闭? I believe it should be set to 1777. 我认为应该将其设置为1777。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM