Skip to content

Commit 06bbd28

Browse files
committed
Use PIPE_READ_END, PIPE_WRITE_END to clarify use of a socketpair
Both sockets in the socket pair are technically bidirectional, but we are using them as though they were unidirectional, with the same convention as pipe(): the first socket in the array is used for reading, and the second is for writing. Signed-off-by: Simon McVittie <smcv@collabora.com>
1 parent 4021ac2 commit 06bbd28

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

bubblewrap.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,11 +3144,11 @@ main (int argc,
31443144
{
31453145
/* Parent, outside sandbox, privileged (initially) */
31463146

3147-
if (intermediate_pids_sockets[0] != -1)
3147+
if (intermediate_pids_sockets[PIPE_READ_END] != -1)
31483148
{
3149-
close (intermediate_pids_sockets[1]);
3150-
pid = read_pid_from_socket (intermediate_pids_sockets[0]);
3151-
close (intermediate_pids_sockets[0]);
3149+
close (intermediate_pids_sockets[PIPE_WRITE_END]);
3150+
pid = read_pid_from_socket (intermediate_pids_sockets[PIPE_READ_END]);
3151+
close (intermediate_pids_sockets[PIPE_READ_END]);
31523152
}
31533153

31543154
/* Discover namespace ids before we drop privileges */
@@ -3232,9 +3232,9 @@ main (int argc,
32323232

32333233
/* We're back, either in a child or grandchild, so message the actual pid to the monitor */
32343234

3235-
close (intermediate_pids_sockets[0]);
3236-
send_pid_on_socket (intermediate_pids_sockets[1]);
3237-
close (intermediate_pids_sockets[1]);
3235+
close (intermediate_pids_sockets[PIPE_READ_END]);
3236+
send_pid_on_socket (intermediate_pids_sockets[PIPE_WRITE_END]);
3237+
close (intermediate_pids_sockets[PIPE_WRITE_END]);
32383238
}
32393239

32403240
/* Child, in sandbox, privileged in the parent or in the user namespace (if --unshare-user).

utils.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,12 @@ send_pid_on_socket (int sockfd)
782782
die_with_error ("Can't send pid");
783783
}
784784

785+
/*
786+
* Create a socket pair such that if one process calls
787+
* send_pid_on_socket(sockets[PIPE_WRITE_END]),
788+
* another process will be able to call
789+
* read_pid_from_socket(sockets[PIPE_READ_END]).
790+
*/
785791
void
786792
create_pid_socketpair (int sockets[2])
787793
{
@@ -790,7 +796,7 @@ create_pid_socketpair (int sockets[2])
790796
if (socketpair (AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != 0)
791797
die_with_error ("Can't create intermediate pids socket");
792798

793-
if (setsockopt (sockets[0], SOL_SOCKET, SO_PASSCRED, &enable, sizeof (enable)) < 0)
799+
if (setsockopt (sockets[PIPE_READ_END], SOL_SOCKET, SO_PASSCRED, &enable, sizeof (enable)) < 0)
794800
die_with_error ("Can't set SO_PASSCRED");
795801
}
796802

0 commit comments

Comments
 (0)