论坛首页> 三共建设

具有写权限却不能写入文件

某天在测试某个设备时,突然发现一个问题,明明对该文件具有写的权限,但是却不能写入信息


$ ls -lh /tmp/my.log

-rw-rw-rw- 1 lys  lys     0 2   8 15:48 my.log


我们是以 jerry 的身份修改 my.log ,代码如下


#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <sys/stat.h>

 

int main(int argc, char const *argv[])

{

       char *name = "/tmp/my.log";

       FILE *fp = fopen(name, "a+");

       if(fp == NULL) {

              fprintf(stderr, "Couldn't open: %s: %s\n", name, strerror(errno));

       }

       else

              fclose(fp);

       return 0;

}

运行该程序

 

$ ./test

Couldn't open: /tmp/my.log: Permission denied

1

2

这个问题来源于在 Linux 4.19 内核引入的一个内核参数 this commit fs.protected_regular,用于禁止在全局可写 Sticky Bit 目录中打开不属于用户的 FIFO 或常规文件。相关文档位于 Documentation/sysctl/fs.txt

 

protected_fifos:

 

The intent of this protection is to avoid unintentional writes to an attacker-controlled FIFO, where a program expected to create a regular file.

 

 

protected_regular:

 

This protection is similar to protected_fifos, but it avoids writes to an attacker-controlled regular file, where a program expected to create one.

 

When set to “0”, writing to regular files is unrestricted.

 

When set to “1” don’t allow O_CREAT open on regular files that we don’t own in world writable sticky directories, unless they are owned by the owner of the directory.

 

When set to “2” it also applies to group writable sticky directories.

 

即这个保护可以用以下命令关闭

 

sysctl fs.protected_regular=0

 

这个特性用于缓解以下漏洞

 

CVE-2000-1134

CVE-2007-3852

CVE-2008-0525

CVE-2009-0416

CVE-2011-4834

CVE-2015-1838

CVE-2015-7442

CVE-2016-7489

由于一些开发者没有注意到该特性,会导致一些空指针问题,例如如下代码

 

 

当一个用户运行此程序,会试图创建该文件,并将该文件的权限修改为 -rw-rw-rw- 。当另外一个用户运行此程序,按照正常逻辑,该文件已经存在,因此不需要创建,直接就会打开,但是其实是空指针。fclose 时报错。

————————————————

版权声明:本文为CSDN博主「江下枫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/song_lee/article/details/122827879

全部回复

暂无更多回复内容