Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix usage of most POSIX functions #65

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

fix usage of most POSIX functions #65

wants to merge 5 commits into from

Conversation

DerDakon
Copy link
Member

  • if standard functions are used, use the correct header(s) for them
  • some reimplemented functions can easily be replaced with their equivalents from the C standard, which will usually result in much better code being generated by the compiler

There are still some functions left where the proper header is not included, e.g. rename(). This function is declared in <stdio.h>, which would collide badly with the custom puts() functions used at multiple places.

@alanpost
Copy link
Contributor

The "get rid of" series in this PR should be submitted separately. The set of steps I need to perform for due diligence differ for that series than from the other commits here, due to at least "4.1 Identifying common functions" in djb's "Some thoughts on security after ten years of qmail 1.0."

That question doesn't even come up for the other patches, hence my request to file separate PRs.

@alanpost
Copy link
Contributor

Would you please pull these commits in to separate PRs:

add missing return types to main()
use ssize_t in substdio

the main return types I have some comments on and would like to see merged shortly. The bottom one is likely going to be necessary on at least one platform, now that #80 is in, and I'd like to give it a similar separate treatment (comments and corrections).

@DerDakon
Copy link
Member Author

Done in #84 and #85.


#define byte_zero(s,n) (memset((s),0,(n)))
Copy link
Member

Choose a reason for hiding this comment

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

In-tree, byte_zero() is called from three places:

  1. qmail-popup.c, with the intent of clearing username/password/timestamp from memory ASAP
  2. timeoutconn.c and remoteinfo.c, to zero out a new struct

For (2), I'm less worried about what compilers will optimize out; this looks like an idiomatic use of memset().

For (1), I'm more worried. How can we tell whether the existing hand-rolled byte_zero() implementation has the desired effect, and whether a memset() preserves the expected behavior?

Out of tree, I know of at least my own authup program that's derived from qmail-popup and has the same expectations for byte_zero().

qmail-newmrh.c Outdated Show resolved Hide resolved
Copy link
Member

@schmonz schmonz left a comment

Choose a reason for hiding this comment

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

Trivial Q about include ordering, and a bigger one about reliably zeroing out sensitive memory.

I'm not getting warnings without the missing includes, probably because the system header is already included transitively. I'm in favor of direct inclusion, though, so as usual I'm curious how you found these (and whether it's something we can/should automate).

@DerDakon
Copy link
Member Author

I'm not getting warnings without the missing includes, probably because the system header is already included transitively. I'm in favor of direct inclusion, though, so as usual I'm curious how you found these (and whether it's something we can/should automate).

Building with -Wall, probably -Wmissing-declarations is enough. That these errors now are not happening is very likely the result of other work that introduced <unistd.h> in some of our headers (e.g. readwrite.h), where it was originally also missing. I still think that we should add them explicitly to these *.c files where we use getpid() or other functions directly.

I have no problem dropping the byte_zero() change from this commit for now and keep it for as long as qmail-popup.c exists, or until we find a better way. The other 2 users could be solved differently if we actually care.

Copy link

@josuah josuah left a comment

Choose a reason for hiding this comment

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

Some more suggestions that could jump onto that merge, but otherwise all in favor of this.

@@ -7,8 +7,8 @@ extern unsigned int byte_chr();
extern unsigned int byte_rchr();
extern void byte_copy();
Copy link

Choose a reason for hiding this comment

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

For example, in qmail-local.c: byte_copy(fnnewtph,3,"new");

So:

#define byte_copy(d,n,s) (memcpy(d,s,n))

Or to preserve semantics:

#define byte_copy(d,n,s) (memmove(d,s,n))

@@ -7,8 +7,8 @@ extern unsigned int byte_chr();
extern unsigned int byte_rchr();
Copy link

Choose a reason for hiding this comment

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

The memrchr is a widely available extension, only memchr is (C89) standard.

byte.h Show resolved Hide resolved
@DerDakon
Copy link
Member Author

DerDakon commented May 5, 2022

So what we have left are 5 commits, 4 of them add sort-of missing headers, and then the somewhat dangerous memset thing. This has been much more at the start, and more and more parts of it have been done through dedicated MRs, and I think it's about time to split the 4 header commits into an own MR, and let the memset() on rest in piece here.

@DerDakon
Copy link
Member Author

DerDakon commented Jul 4, 2022

The relevant commits have been moved to #237, I think the remainder can be dropped.

  • byte_zero() is kept to make it more likely that the zeroing of the password buffer is not optimized out
  • <sys/types.h> is not really needed for umask() as no variable is used for the return value or the argument

@schmonz schmonz modified the milestones: 1.09, 1.90 Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants