为了保护应用程序不被错误地使用,我正在尝试检查其配置文件是否具有正确的权限,以便应用程序可以信任其他人未修改的文件内容。
我相信以下规则是正确的:
这是一个例子:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
static
int is_secure(const char *name)
{
struct stat st;
uid_t euid = geteuid();
gid_t egid = getegid();
if (stat(name, &st) != 0) {
int err = errno;
fprintf(stderr, "can't stat() '%s': %d (%s)\n", name, err, strerror(err));
return 0;
}
/* writable by other: unsecure */
if ((st.st_mode & S_IWOTH) != 0) {
return 0;
}
/* not owned by group root and not owned by effective group: unsecure */
if (st.st_gid != 0 && st.st_gid != egid) {
return 0;
}
/* not owned by user root and not owned by effective user: unsecure */
if (st.st_uid != 0 && st.st_uid != euid) {
return 0;
}
return 1;
}
int
main(int argc, char *argv[])
{
int i;
for(i = 1; i < argc; i++) {
printf("'%s' : %s\n", argv[i], is_secure(argv[i]) ? "sure" : "unsure");
}
return 0;
}
由于我不确定我的假设,有人可以检查我是否在文件权限检查中留下了一些漏洞。
更新
sudo有一个函数:sudo_secure_path,它只检查一个uid / gid,但它负责检查组写位。
问候。
答案 0 :(得分:8)
您的规则和代码对我来说是正确的,但您应该了解以下可能仍会影响您的实施的安全风险。
正如您所看到的,这些不是代码控制下的问题。因此,在确保客户端确认配置文件不可篡改之前,应确保客户端了解这些风险。
答案 1 :(得分:7)
我相信您还想检查目录的权限。
如果允许用户写入目录,则用户可以mv
另一个属于正确用户的文件替换此文件。
类似的东西:
sudo touch foo.conf
sudo touch foo.conf-insecure-sample
mv -f foo.conf-insecure-sample foo.conf