我们正在尝试在项目级别将Dev用户配置为仅具有“查看者”访问权限,并且还允许他们登录Cloud SQL。 奇怪的是,没有像DataStore或Bigquery那样的精细权限。 在将Cloud SQL代理配置为遵循Google最佳做法以连接到V2 Cloud SQL实例后尝试进行连接时。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在运行具有 sudo 访问权限的用户的 C++ 程序。 二进制文件是用./binName
命令启动的。
从它我可以用popen
function 执行sudo ls
- 这工作正常。 所以我虽然我可以用open
的 function 打开一些特权文件,但它返回 -1 和 errno= 13:Permission denied。
所以然后我虽然我需要用 seteuid 强制设置我的seteuid
但它返回 -1 和 errno= 13:Permission denied
当我使用sudo./binName
启动程序时,一切正常,我可以使用open
的 function 打开任何文件。 我做错了什么?
#include <stdio.h>
#include <vector>
#include <cstdint>
#include <cstring>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#define Log(fmt, ...) printf("%s | " fmt, __FUNCTION__, ##__VA_ARGS__)
bool OpenPath(const std::string& path) {
if (path.empty()) return false;
const int f = open(path.c_str(), O_RDWR);
if (f == -1){
Log("[!] Unable to open %s, errno= %d:%s\n", path.c_str(), errno, strerror(errno));
return false;
}
close(f);
return true;
}
bool ReadFromStream(const char *command, std::vector<uint8_t> &output)
{
FILE *fp = popen(command, "r");
if (!fp)
{
Log("[!] Error: Unable to popen\n");
return false;
}
char str[512];
while (!feof(fp))
{
if (fgets(str, sizeof(str), fp))
{
const int size = strlen(str);
output.insert(output.end(), str, str + size);
}
}
pclose(fp);
return !output.empty();
}
bool HasRights() {
const __uid_t uid = getuid();
const __uid_t gid = geteuid();
bool elevated = uid == 0 || uid != gid;
Log("uid= %d, gid= %d, elevated= %d\n", uid, gid, elevated);
if (elevated && OpenPath("/usr/bin/ls")) // it's just a test path
return true;
std::vector<uint8_t> buffer;
if (ReadFromStream("sudo -n ls", buffer)) {
const std::string str(buffer.begin(), buffer.end());
if (str != "sudo: a password is required")
{
if (seteuid(0) == -1) {
Log("[!] Unable seteuid, errno= %d:%s\n", errno, strerror(errno));
}
return OpenPath("/usr/bin/ls"); // it's just a test path
}
}
return false;
}
int main(int argc, char **argv)
const auto hasRights = HasRights();
Log("hasRights= %d\n", hasRights);
return 0;
}
output 没有 sudo 权限=./binName
HasRights | uid= 1000, gid= 1000, elevated= 0
HasRights | [!] Unable seteuid, errno= 1:Operation not permitted
OpenPath | [!] Unable to open /usr/bin/ls, errno= 13:Permission denied
test | hasRights= 0
output 具有 sudo 权限= sudo./binName
HasRights | uid= 0, gid= 0, elevated= 1
test | hasRights= 1
output 当 sudo 超时过期=./binName
HasRights | uid= 1000, gid= 1000, elevated= 0
sudo: a password is required
test | hasRights= 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.