Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/auth/passdb-pam.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ pam_userpass_conv(int num_msg, pam_const struct pam_message **msg,

*resp_r = NULL;

resp = calloc(num_msg, sizeof(struct pam_response));
if (num_msg < 0 ||
(size_t)num_msg > SIZE_MAX / sizeof(struct pam_response)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calloc() already checks for integer overflows, and returns NULL if there would be an overflow. And Dovecot handles NULL already by failing with out-of-memory. Other large num_msgs are similarly handled as NULL -> out-of-memory. I guess there's the question of whether we should enforce some sane limit, but since PAM is a very trusted source I don't think it makes practically any difference.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that calloc() already handles overflow and that the NULL
path is properly handled here, so this doesn’t introduce a practical
issue as-is.

Looking at it again, the more relevant concern here would be avoiding
excessive allocations if num_msg is unexpectedly large. Even if PAM is
generally trusted, adding a reasonable upper bound could make this path
more robust and easier to reason about.

I can update this PR to add a limit check instead, or drop the change
if you'd prefer to keep the current behavior.

e_error(authdb_event(ctx->request),
"pam: invalid response count: %d", num_msg);
return PAM_CONV_ERR;
}

resp = calloc((size_t)num_msg, sizeof(struct pam_response));
if (resp == NULL)
i_fatal_status(FATAL_OUTOFMEM, "Out of memory");

Expand Down