Bug 2073743

Summary: getopt_long: manual clarification: getopt_long long option names may be abbreviated
Product: Red Hat Enterprise Linux 9 Reporter: Abdul Rehman Quadri <aquadri>
Component: glibcAssignee: glibc team <glibc-bugzilla>
Status: CLOSED ERRATA QA Contact: Sergey Kolosov <skolosov>
Severity: medium Docs Contact:
Priority: medium    
Version: 9.0CC: aboscatt, ashankar, codonell, cww, dj, fweimer, ipedrosa, mnewsome, pbrezina, pfrankli, sipoyare
Target Milestone: rcKeywords: Bugfix, Patch, Triaged
Target Release: ---   
Hardware: All   
OS: Linux   
URL: http//
Whiteboard:
Fixed In Version: glibc-2.34-35.el9 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-11-15 11:11:50 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 2091541    
Bug Blocks: 2010412    

Description Abdul Rehman Quadri 2022-04-10 05:46:06 UTC
Description of problem:
1. The useradd command now allows names that do not conform to standards using the option "--badnames". 

2. Now if the option --bad is used then also it allows the user to be created with bad names. The below options are being resulted in successful user account creation by useradd:

    2.1: --bad
    2.2: --badn
    2.3: --badna
    2.4: --badnam
    2.5: --badname
    2.6: --badnames


Version-Release number of selected component (if applicable):
shadow-utils-4.9-3.el9.x86_64

How reproducible:
Always

Steps to Reproduce:
1. log in as a privileged/root user
2. useradd --bad @user1
3. useradd --badn @user2
4. useradd --badna @user3
5. useradd --badnam @user4
6. useradd --badname @user5
7. useradd --badnames @user6

Actual results:
All the users from user1 to user6 get added successfully

Expected results:
Steps 2-6 should be failed I think and it should throw warning message and only the 7th step should get succeeded i.e the useradd command should only allow adding a user with badname with a single option "--badnames" 

Additional info:

Comment 1 Iker Pedrosa 2022-04-11 07:16:20 UTC
*** Bug 2069680 has been marked as a duplicate of this bug. ***

Comment 2 Iker Pedrosa 2022-04-11 07:19:06 UTC
As mentioned in https://bugzilla.redhat.com/show_bug.cgi?id=2069680#c1 the good option is "badnames", so the other ones shouldn't work.

As you are playing around, can you try "--ba" and "--badnamesblablabla" options?

Comment 3 Abdul Rehman Quadri 2022-04-11 09:17:32 UTC
(In reply to Iker Pedrosa from comment #2)
> As mentioned in https://bugzilla.redhat.com/show_bug.cgi?id=2069680#c1 the
> good option is "badnames", so the other ones shouldn't work.
> Thank you for the comment.


> As you are playing around, can you try "--ba" and "--badnamesblablabla"
> options?
> Yeah, Tried that and it is not working, "--ba" and "--badnamesblablabla" both the options are showing invalid.

Comment 4 Iker Pedrosa 2022-05-02 08:58:06 UTC
shadow-utils is using the getopt_long() API to parse the command line arguments. I would expect this API to only match the exact string with an option, but it is matched even if some characters are missing (--bad, --badn, --badna, etc.). I don't see any reference to this in the man page. Siddhesh could you please clarify if this is intentional?

Comment 5 Siddhesh Poyarekar 2022-05-03 04:52:32 UTC
(In reply to Iker Pedrosa from comment #4)
> shadow-utils is using the getopt_long() API to parse the command line
> arguments. I would expect this API to only match the exact string with an
> option, but it is matched even if some characters are missing (--bad,
> --badn, --badna, etc.). I don't see any reference to this in the man page.
> Siddhesh could you please clarify if this is intentional?

There is no reference to this behaviour in the glibc manual, but there is in the man page and in code comments.  Excerpt from the `man getopt_long` on F35:

getopt_long() and getopt_long_only()
    ...
    Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option.
    ...

Comment 6 Siddhesh Poyarekar 2022-05-03 05:00:13 UTC
(In reply to Siddhesh Poyarekar from comment #5)
> (In reply to Iker Pedrosa from comment #4)
> > shadow-utils is using the getopt_long() API to parse the command line
> > arguments. I would expect this API to only match the exact string with an
> > option, but it is matched even if some characters are missing (--bad,
> > --badn, --badna, etc.). I don't see any reference to this in the man page.
> > Siddhesh could you please clarify if this is intentional?
> 
> There is no reference to this behaviour in the glibc manual, but there is in
> the man page and in code comments.  Excerpt from the `man getopt_long` on
> F35:
> 
> getopt_long() and getopt_long_only()
>     ...
>     Long option names may be abbreviated if the abbreviation is unique or is
> an exact match for some defined option.
>     ...

... and to close the gap, I've posted a glibc patch upstream to also document this behaviour in the glibc manual:

https://patchwork.sourceware.org/project/glibc/patch/20220503045743.2887439-1-siddhesh@sourceware.org/

Comment 18 errata-xmlrpc 2022-11-15 11:11:50 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (glibc bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:8272