c - UNIX 套接字权限 (Linux)

标签 c linux sockets unix

我正在使用 C 中的 UNIX 套接字开发服务器。来自手册:

In the Linux implementation, sockets which are visible in the filesystem honor the per‐ missions of the directory they are in. Their owner, group and their permissions can be changed. Creation of a new socket will fail if the process does not have write and search (execute) permission on the directory the socket is created in. Connecting to the socket object requires read/write permission. This behavior differs from many BSD- derived systems which ignore permissions for UNIX domain sockets. Portable programs should not rely on this feature for security.

我有一条世界可写的路径。

    $ ls -ld api
    drwxrwxrwx 2 root www-data 4096 Feb 15 21:57 api

root 下的进程在此路径中创建套接字:

    $  ls -l api/socket
    srwxr-xr-x 1 root root 0 Feb 15 21:57 api/socket

由于权限问题,作为用户运行的另一个进程无法连接到套接字。如果我手动将套接字权限更改为所有人都可写,那么其他进程就可以成功连接。

  1. 为什么父权限不足以使套接字像文档所说的那样可写?
  2. 在这种情况下,最佳做法是什么?

最佳答案

<强>1。为什么父权限不足以使套接字像文档所说的那样可写?

The doc

Connecting to the socket object requires read/write permission.

父权限只与新套接字的创建有关,这就是文档所说的全部内容:

Creation of a new socket will fail if the process does not have write and search (execute) permission on the directory the socket is created in.

您可以随意让您的套接字可写:

Their owner, group and their permissions can be changed.

<强>2。在这种情况下,最佳做法是什么?

创建一个套接字,并使其成为用户的 (man 2 chown)。或者创建一个套接字,并使其可写(man 2 chmod)。

关于c - UNIX 套接字权限 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424970/

相关文章:

python - 如何从类中调用外部函数 - python

c - 在 C 中使用 argv 和 argc 解析命令行

我们是否可以通过改变 if 条件,在执行完 else 条件后返回执行 if 条件?

linux - 科学Linux : I need to take a shell script variable and use it to search in a awk script

node.js - docker 里面的僵尸

c - 如何使用 NodeMCU 通过 TCP 发送/接收二进制数据?

java - SocketImpl Setoption 中的套接字关闭异常

c - 使用 opengl 在游戏过程中拍摄快照

C89 和变量初始化

linux - 阻止 Perl 的 `tie` 在我的串行接口(interface)上​​重置我的 Arduino/脉冲 DTR