Yan*_*aud 9 c unix security permissions file
为了保护应用程序不被错误地使用,我正在尝试检查其配置文件是否具有正确的权限,以便应用程序可以信任未被其他人修改的文件的内容.
我相信以下规则是正确的:
这是一个例子:
#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;
}
Run Code Online (Sandbox Code Playgroud)
由于我不确定我的假设,有人可以检查我是否在文件权限检查中留下了一些漏洞.
更新
sudo有一个函数:sudo_secure_path,它只检查一个uid/gid,但它负责检查组写位.
问候.
您的规则和代码对我来说是正确的,尽管您应该了解以下可能仍会影响实施的安全风险.
正如您所看到的,这些不是代码控制下的问题.因此,在确保客户端确认配置文件不可篡改之前,应确保客户端了解这些风险.
小智 7
我相信你也想检查目录的权限.
用户将能够mv属于正确的用户更换这一项,如果他们被允许写入该目录另一个文件.
就像是:
sudo touch foo.conf
sudo touch foo.conf-insecure-sample
mv -f foo.conf-insecure-sample foo.conf
Run Code Online (Sandbox Code Playgroud)