Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C setuid()失败-不允许操作_C_Linux_Uid_Setuid - Fatal编程技术网

C setuid()失败-不允许操作

C setuid()失败-不允许操作,c,linux,uid,setuid,C,Linux,Uid,Setuid,changeIDs()正在尝试使用setuid()更改有效的用户id 但它总是出错,我不知道为什么 我有两个电脑用户。用户是UID为1000的管理员。另一个标准用户用户2的UID为1001 我想用这个程序将user2的有效UID设置为user1(1000)。为什么setuid()总是出错 我确保在程序可执行文件上运行chmodu+s,但仍然失败 error with setuid()-errno:不允许操作 还有,你知道为什么我的perror弦中的E是截断的吗 定义GNU源 #包括 #包括 #包

changeIDs()正在尝试使用setuid()更改有效的用户id 但它总是出错,我不知道为什么

我有两个电脑用户。用户是UID为1000的管理员。另一个标准用户用户2的UID为1001

我想用这个程序将user2的有效UID设置为user1(1000)。为什么setuid()总是出错

我确保在程序可执行文件上运行chmodu+s,但仍然失败

error with setuid()-errno:不允许操作

还有,你知道为什么我的perror弦中的E是截断的吗

定义GNU源
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void getArguments(int argc,char**argv);
void displayIDs();
void changeID(int userid);
int main(int argc,字符**argv)
{
getArguments(argc、argv);
返回0;
}
/*
*程序接受一个选项“c”,后跟一个数字用户id。
*当使用c选项和用户id执行程序时,
*系统显示真实的、有效的和保存的设置用户id,
*然后尝试将有效用户id更改为数字用户
*id传递到应用程序中,然后显示真实的,
*有效且已保存的设置用户id(20分)
*/
void changeID(int userid)
{
printf(“原始ID:\n============================\n”);
displayIDs();
uid\u t newid=(uid\u t)userid;
//如setuid()手册页中所述,将id变量作为引用传递
//错误检查,失败返回-1
/*
if(setresuid(&newid,&newid,&newid)=-1)
{
perror(“setuid()错误-errno”+errno);
}
*/
if(setuid(&newid)=-1)
{
perror(“setuid()错误-errno”+errno);
}
printf(“\n(尝试)更改的ID:\n=========================\n”);
displayIDs();
}
/*
*程序接受“g”选项
*使用g选项执行程序时,
*系统显示真实有效的,
*和保存的设置用户id(10分)
*/
void displayIDs()
{
uid\u t ruid;//实际用户id
uid\u t euid;//有效用户id
uid\u t suid;//保存的集合id
//将id变量作为引用传递,如getresuid()手册页中所述
//错误检查,失败返回-1
if(getresuid(&ruid,&euid,&suid)=-1)
{
perror(“getresuid()错误-errno”+errno);
}
printf(“实际用户ID:%d\n”,ruid);
printf(“有效用户ID:%d\n”,euid);
printf(“保存集用户ID:%d\n”,suid);
}
//从命令行获取参数并将其传递到程序中,调用正确的函数
void getArguments(int argc,char**argv)
{
int选项=0;
而((option=getopt(argc,argv,“gc:)!=-1)
{
开关(选件)
{
案例“g”:
displayIDs();
打破
案例“c”:
变更ID(optarg);
打破
案例“?”:
printf(“无效参数\n”);
打破
违约:
printf(“无效-无参数(g或c)\n”);
打破
}
}
}

它失败,因为您要设置的有效用户ID既不是0(root),也不是与您期望的ID相同

下面来自
setuid(2)

函数的作用是:设置真实有效的用户ID和 保存的 将当前进程的用户ID设置为指定值。setuid() 如果有效用户ID是超级用户的ID,则允许使用该功能, 或者如果指定的用户ID与有效用户ID相同。如果 不是,但指定的用户ID与实际用户ID setuid()相同 将有效用户ID设置为真实用户ID

error with setuid()-errno:不允许操作

还有,你知道为什么我的perror弦中的E是截断的吗

这是因为您传递了
perror()
“Error with setuid()-errno”+errno
,这相当于
&“Error with setuid()-errno”[errno]
,它(因为
errno
等于1)等于字符串的第二个
字符的地址。

你似乎已经习惯了一种带有连接运算符的语言,而C语言却不是这样。

我想你误解了
setuid
的作用-它不会改变系统上用户的uid。它更改当前进程的用户id,使其看起来像是由其他用户运行的。通常,只有root用户可以这样做。
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>

void getArguments(int argc, char **argv);
void displayIDs();
void changeID(int userid);



int main(int argc, char **argv)
{
    getArguments(argc, argv);

    return 0;
}



/*
 * The program accepts an option of “c” followed by a numeric user id.
 * When executing the program with the c option followed by a user id,
 * the system displays the real, effective, and saved set user id,
 * then attempts to change the effective user id to the numeric user
 * id passed into the application, and then displays the real,
 * effective, and saved set user id. (20 pts)
 */
void changeID(int userid)
{
    printf("Original IDs:\n==================\n");
    displayIDs();

    uid_t newid = (uid_t)userid;

    //pass the id var as references as outlined in the setuid() man pages
    //error check, fail returns -1
    /*
    if(setresuid(&newid, &newid, &newid) == -1)
    {
        perror("Error with setuid() - errno " + errno);
    }
    */


    if(setuid(&newid) == -1)
    {
        perror("Error with setuid() - errno " + errno);
    }


    printf("\n(Attempted) Changed IDs:\n==================\n");
    displayIDs();
}



/*
 * The program accepts an option of “g.”
 * When executing the program with the g option,
 * the system displays the real, effective,
 * and saved set user id. (10 pts)
 */
void displayIDs()
{
    uid_t ruid;//real user id
    uid_t euid;//effective user id
    uid_t suid;//saved set id

    //pass the id vars as references as outlined in the getresuid() man pages
    //error check, fail returns -1
    if ( getresuid(&ruid, &euid, &suid) == -1)
    {
        perror("Error with getresuid() - errno " + errno);
    }

    printf("Real User ID: %d\n", ruid);
    printf("Effective User ID: %d\n", euid);
    printf("Saved Set User ID: %d\n", suid);
}



//get the arguments from the command line and pass it into the program, calling the right function
void getArguments(int argc, char **argv)
{
    int option = 0;

    while ((option = getopt(argc, argv, "gc:")) != -1)
    {

        switch (option)
        {
             case 'g' :
                 displayIDs();
                 break;
             case 'c' :
                 changeID(optarg);
                 break;
             case '?' :
                 printf("Invalid argument\n");
                 break;
             default:
                 printf("Invalid - no argument (g or c)\n");
                 break;
        }
    }
}