Skip to content

Commit ae23064

Browse files
committed
Merge branch 'af_unix-followup-fixes-for-so_passpidfd'
Kuniyuki Iwashima says: ==================== af_unix: Followup fixes for SO_PASSPIDFD. This series fixes 2 issues introduced by commit 5e2ff67 ("scm: add SO_PASSPIDFD and SCM_PIDFD"). The 1st patch fixes a warning in scm_pidfd_recv() reported by syzkaller. The 2nd patch fixes a regression that bluetooth can't be built as module. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 30ac666 + a9c49cc commit ae23064

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

include/net/scm.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ static __inline__ void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm
135135
return;
136136
}
137137

138-
WARN_ON_ONCE(!scm->pid);
138+
if (!scm->pid)
139+
return;
140+
139141
pidfd = pidfd_prepare(scm->pid, 0, &pidfd_file);
140142

141143
if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
@@ -151,16 +153,16 @@ static __inline__ void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm
151153
fd_install(pidfd, pidfd_file);
152154
}
153155

154-
static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
155-
struct scm_cookie *scm, int flags)
156+
static inline bool __scm_recv_common(struct socket *sock, struct msghdr *msg,
157+
struct scm_cookie *scm, int flags)
156158
{
157159
if (!msg->msg_control) {
158160
if (test_bit(SOCK_PASSCRED, &sock->flags) ||
159161
test_bit(SOCK_PASSPIDFD, &sock->flags) ||
160162
scm->fp || scm_has_secdata(sock))
161163
msg->msg_flags |= MSG_CTRUNC;
162164
scm_destroy(scm);
163-
return;
165+
return false;
164166
}
165167

166168
if (test_bit(SOCK_PASSCRED, &sock->flags)) {
@@ -173,19 +175,34 @@ static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
173175
put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds);
174176
}
175177

176-
if (test_bit(SOCK_PASSPIDFD, &sock->flags))
177-
scm_pidfd_recv(msg, scm);
178+
scm_passec(sock, msg, scm);
178179

179-
scm_destroy_cred(scm);
180+
if (scm->fp)
181+
scm_detach_fds(msg, scm);
180182

181-
scm_passec(sock, msg, scm);
183+
return true;
184+
}
182185

183-
if (!scm->fp)
186+
static inline void scm_recv(struct socket *sock, struct msghdr *msg,
187+
struct scm_cookie *scm, int flags)
188+
{
189+
if (!__scm_recv_common(sock, msg, scm, flags))
184190
return;
185-
186-
scm_detach_fds(msg, scm);
191+
192+
scm_destroy_cred(scm);
187193
}
188194

195+
static inline void scm_recv_unix(struct socket *sock, struct msghdr *msg,
196+
struct scm_cookie *scm, int flags)
197+
{
198+
if (!__scm_recv_common(sock, msg, scm, flags))
199+
return;
200+
201+
if (test_bit(SOCK_PASSPIDFD, &sock->flags))
202+
scm_pidfd_recv(msg, scm);
203+
204+
scm_destroy_cred(scm);
205+
}
189206

190207
#endif /* __LINUX_NET_SCM_H */
191208

net/unix/af_unix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
24272427
}
24282428
err = (flags & MSG_TRUNC) ? skb->len - skip : size;
24292429

2430-
scm_recv(sock, msg, &scm, flags);
2430+
scm_recv_unix(sock, msg, &scm, flags);
24312431

24322432
out_free:
24332433
skb_free_datagram(sk, skb);
@@ -2808,7 +2808,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
28082808

28092809
mutex_unlock(&u->iolock);
28102810
if (state->msg)
2811-
scm_recv(sock, state->msg, &scm, flags);
2811+
scm_recv_unix(sock, state->msg, &scm, flags);
28122812
else
28132813
scm_destroy(&scm);
28142814
out:

0 commit comments

Comments
 (0)