Skip to content

Commit 36d3dad

Browse files
committed
Return EBADMSG on recvmsg len mismatch
Allows us to distinguish from a shortened header. Log the result and expected sizes to try and diagnose.
1 parent 595c305 commit 36d3dad

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/privsep-root.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,17 @@ ps_root_readerrorcb(struct psr_ctx *psr_ctx)
126126
}
127127
}
128128
if (psr_error->psr_datalen != 0) {
129+
/* Set our buffers */
129130
if (psr_ctx->psr_usemdata) {
130131
iov[1].iov_base = psr_ctx->psr_mdata;
131-
/* psr_mdatalen could be smaller then psr_datalen
132-
* if the above malloc failed. */
133-
iov[1].iov_len =
134-
MIN(psr_ctx->psr_mdatalen, psr_error->psr_datalen);
132+
iov[1].iov_len = psr_ctx->psr_mdatalen;
135133
} else {
136134
iov[1].iov_base = psr_ctx->psr_data;
137-
/* This should never be the case */
138-
iov[1].iov_len =
139-
MIN(psr_ctx->psr_datalen, psr_error->psr_datalen);
135+
iov[1].iov_len = psr_ctx->psr_datalen;
140136
}
137+
/* We might require less than the buffer size */
138+
if (iov[1].iov_len > psr_error->psr_datalen)
139+
iov[1].iov_len = psr_error->psr_datalen;
141140
}
142141

143142
recv:
@@ -151,8 +150,11 @@ ps_root_readerrorcb(struct psr_ctx *psr_ctx)
151150
PSR_ERROR(EINVAL);
152151
else if (msg.msg_flags & MSG_TRUNC)
153152
PSR_ERROR(ENOBUFS);
154-
else if ((size_t)len != sizeof(*psr_error) + psr_error->psr_datalen)
155-
PSR_ERROR(EINVAL);
153+
else if ((size_t)len != sizeof(*psr_error) + psr_error->psr_datalen) {
154+
logerrx("%s: recvmsg returned %zd, expecting %zu", __func__,
155+
len, sizeof(*psr_error) + psr_error->psr_datalen);
156+
PSR_ERROR(EBADMSG);
157+
}
156158
return len;
157159
}
158160

0 commit comments

Comments
 (0)