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

Various fixes related to string copying #681

Merged
merged 9 commits into from
Mar 28, 2023

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Mar 13, 2023

This consists of some patches from @eggert, and some patches of mine which have been more or less based on his suggestions. See the origin of the discussion at Debbugs: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1032393#159.

I didn't intend to remove strlcpy(3), since the bugs are independent of the use of strlcpy(3). I opted for fixing the bugs, and keeping strlcpy(3) where it's (IMO) the most appropriate call.

We might want to drop strlcpy(3), but I think that should be addressed separately, and after having fixed the bugs.

There's one patch from Paul that I didn't take or write an alternative, and that's because I didn't yet understand it. It's Avoid silent truncation of console file data (see Debbugs above).

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Mar 13, 2023

Changes:

  • Don't else after [[noreturn]]

@alejandro-colomar
Copy link
Collaborator Author

Changes:

  • Add STRLEN() for fixing VLA issues (strlen(3) doesn't produce a constant expression)

libmisc/date_to_str.c Show resolved Hide resolved
@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Mar 13, 2023

Changes:

  • Use less bytes for the definition of STRLEN().

@alejandro-colomar alejandro-colomar marked this pull request as ready for review March 13, 2023 01:13
Copy link
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

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

Only a minor question.

libmisc/date_to_str.c Show resolved Hide resolved
libmisc/date_to_str.c Show resolved Hide resolved
@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Mar 17, 2023 via email

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Mar 17, 2023 via email

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Mar 18, 2023 via email

Copy link
Collaborator

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

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

Thanks for the clarification. Now it makes more sense. LGTM.

eggert and others added 9 commits March 22, 2023 01:08
* lib/fields.c (change_field): Since we know the string fits,
use strcpy(3) rather than strlcpy(3).

Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
* fields.c (change_field): Omit unnecessary test.

Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
* lib/fields.c (change_field): Don't point
before array start; that has undefined behavior.

Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
* lib/gshadow.c (sgetsgent): Use strcpy(3) not strlcpy(3),
since the string is known to fit.

Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
*  libmisc/date_to_str.c (date_to_str): Do not crash if gmtime(3)
   returns NULL because the timestamp is far in the future.

Reported-by: Paul Eggert <eggert@cs.ucla.edu>
Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
*  libmisc/utmp.c (is_my_tty): Declare the parameter as a char array,
   not char *, as it is not necessarily null-terminated.
   Avoid a read overrun when reading 'tty', which comes from
   'ut_utname'.

Reported-by: Paul Eggert <eggert@cs.ucla.edu>
Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
This commit will serve to document why we shouldn't worry about the
truncation in the call to strlcpy(3).  Since we have one more byte in
tmptty than in full_tty, truncation will produce a string that is at
least one byte longer than full_tty.  Such a string could never compare
equal, so we're actually handling the truncation in a clever way.  Maybe
too clever, but that's why I'm documenting it here.

Now, about the simplification itself:

Since we made sure that both full_tty and tmptty are null-terminated, we
can call strcmp(3) instead of strncmp(3).  We can also simplify the
return logic avoiding one branch.

Cc: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
*  src/su.c (check_perms): Do not silently truncate user name.

Reported-by: Paul Eggert <eggert@cs.ucla.edu>
Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
@alejandro-colomar
Copy link
Collaborator Author

Changes:

  • Rebased to master.
  • Add reviewed-by tags
$ git range-diff 6202fb1f^..gh/strlcpy master..strlcpy 
 1:  6202fb1f !  1:  8f4e72ff Simplify change_field() by using strcpy
    @@ Commit message
     
         Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## lib/fields.c ##
     @@ lib/fields.c: void change_field (char *buf, size_t maxsize, const char *prompt)
 2:  b4a9e1f6 !  2:  537cd8f8 Omit unneeded test in change_field()
    @@ Commit message
     
         Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## lib/fields.c ##
     @@ lib/fields.c: void change_field (char *buf, size_t maxsize, const char *prompt)
 3:  f24bf2ce !  3:  a867e81a Fix change_field() buffer underrun
    @@ Commit message
     
         Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## lib/fields.c ##
     @@ lib/fields.c: void change_field (char *buf, size_t maxsize, const char *prompt)
 4:  67be4a8f !  4:  968e60db Prefer strcpy(3) to strlcpy(3) when either works
    @@ Commit message
     
         Signed-off-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## lib/gshadow.c ##
     @@ lib/gshadow.c: void endsgent (void)
 5:  10c2d51f !  5:  ef340fe0 Fix crash with large timestamps
    @@ Commit message
         Reported-by: Paul Eggert <eggert@cs.ucla.edu>
         Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## libmisc/date_to_str.c ##
     @@
 6:  1b95ac98 !  6:  50f9ce97 Add STRLEN(): a constexpr strlen(3) for string literals
    @@ Commit message
         Add STRLEN(): a constexpr strlen(3) for string literals
     
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## lib/defines.h ##
     @@ lib/defines.h: static inline void memzero(void *ptr, size_t size)
 7:  5915e8bd !  7:  6c3e9a93 Fix is_my_tty() buffer overrun
    @@ Commit message
         Reported-by: Paul Eggert <eggert@cs.ucla.edu>
         Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## libmisc/utmp.c ##
     @@
 8:  10596ae1 !  8:  1c77312f Simplify is_my_tty()
    @@ Commit message
     
         Cc: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## libmisc/utmp.c ##
     @@ libmisc/utmp.c: static bool is_my_tty (const char tty[UT_LINESIZE])
 9:  bc55eae6 !  9:  1c0e0124 Fix su(1) silent truncation
    @@ Commit message
         Reported-by: Paul Eggert <eggert@cs.ucla.edu>
         Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
     
      ## src/su.c ##
     @@ src/su.c: static /*@only@*/struct passwd * check_perms (void)

@ikerexxe ikerexxe merged commit 4c210a2 into shadow-maint:master Mar 28, 2023
@alejandro-colomar alejandro-colomar deleted the strlcpy branch April 18, 2023 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants