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

spwd returning different value depending on privileges #54597

Closed
giampaolo opened this issue Nov 11, 2010 · 21 comments
Closed

spwd returning different value depending on privileges #54597

giampaolo opened this issue Nov 11, 2010 · 21 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@giampaolo
Copy link
Contributor

BPO 10388
Nosy @loewis, @pitrou, @giampaolo, @rbtcollins, @bitdancer, @serhiy-storchaka, @vajrasky
Files
  • fix_error_message_getspall.patch
  • fix_error_message_getspall_v2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2010-11-11.14:03:10.753>
    labels = ['type-bug', 'library']
    title = 'spwd returning different value depending on privileges'
    updated_at = <Date 2015-07-25.19:11:49.548>
    user = 'https://github.com/giampaolo'

    bugs.python.org fields:

    activity = <Date 2015-07-25.19:11:49.548>
    actor = 'rbcollins'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2010-11-11.14:03:10.753>
    creator = 'giampaolo.rodola'
    dependencies = []
    files = ['31384', '33404']
    hgrepos = []
    issue_num = 10388
    keywords = ['patch']
    message_count = 19.0
    messages = ['120950', '195689', '195692', '207793', '207839', '207841', '207843', '207846', '207856', '207860', '207863', '233254', '233260', '233261', '233266', '233289', '247376', '247377', '247378']
    nosy_count = 7.0
    nosy_names = ['loewis', 'pitrou', 'giampaolo.rodola', 'rbcollins', 'r.david.murray', 'serhiy.storchaka', 'vajrasky']
    pr_nums = []
    priority = 'low'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10388'
    versions = ['Python 3.6']

    @giampaolo
    Copy link
    Contributor Author

    As root:

    >>> import spwd
    >>> spwd.getspall()
    [spwd.struct_spwd(sp_nam='root', sp_pwd='!', sp_lstchg=14895, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
     ...
    ]

    As limited user:

    >>> import spwd
    >>> spwd.getspall()
    []
    >>> 

    Wouldn't it be better for consistency to raise OSError EACCES instead?

    @serhiy-storchaka
    Copy link
    Member

    See also bpo-18787.

    @vajrasky
    Copy link
    Mannequin

    vajrasky mannequin commented Aug 20, 2013

    Attached the patch to raise OSError instead of returning empty list if a normal user uses getspall function of spwd module.

    I investigated about this issue. There exists situation where /etc/shadow file does not exist:
    https://bugs.kde.org/show_bug.cgi?id=117202
    https://dev.openwrt.org/ticket/2887

    So I think it is better to differentiate the result of the function where a normal user trying to access /etc/shadow file (permission denied, therefore should raise exception) or root user trying to access non-existent /etc/shadow file (therefore should get empty list).

    @giampaolo
    Copy link
    Contributor Author

    Looking back at this: considering we may get errors != EACCESS I think we better be as generic as possible as in:

    --- a/Modules/spwdmodule.c
    +++ b/Modules/spwdmodule.c
    @@ -153,6 +153,8 @@
         if ((d = PyList_New(0)) == NULL)
             return NULL;
         setspent();
    +    if (errno != 0)
    +        return PyErr_SetFromErrno(PyExc_OSError);
         while ((p = getspent()) != NULL) {
             PyObject *v = mkspent(p);
             if (v == NULL || PyList_Append(d, v) != 0) {

    As for 2.7 and 3.3 I have a little concern in terms of backward compatibility as the user will suddenly receive an exception instead of [] but I think that for the sake of correctness the cost is justified.

    @vajrasky
    Copy link
    Mannequin

    vajrasky mannequin commented Jan 10, 2014

    Here is the updated patch addressing Giampaolo's concern.

    @giampaolo
    Copy link
    Contributor Author

    LGTM

    @serhiy-storchaka
    Copy link
    Member

    I doubt that we can change behavior at this time.

    @giampaolo
    Copy link
    Contributor Author

    You mean this should be made in 3.4 only?

    @serhiy-storchaka
    Copy link
    Member

    Even for 3.4 it looks too late.

    But I afraid we can't change this behavior never. Users of spwd don't expect
    any exception, raising an exception will break any existing code.

    Only one safe way is design new API and deprecate old API. This will be great
    refactoring, it should also change APIs of the pwd and grp modules, perhaps
    these three modules should be merged, perhaps we should cross-platform API
    (available on Windows).

    @giampaolo
    Copy link
    Contributor Author

    I can agree with you that it's probably better to avoid changing existent python versions to avoid breakage but I see no need to be that strict for newer ones.

    Current version should check errno but it doesn't. IMO that is clearly a bug.
    Same goes for returning an empty list when users actually exists and for *not* raising FileNotFoundError if the shadow file is not available.

    Also note that the doc does not mention that an empty list should be treated as an alias for "insufficient permissions" or "shadow file not available", therefore whoever made that assumption is someone who deliberately and erroneously decided to do so.

    @bitdancer
    Copy link
    Member

    I think that a new cross platform API would be great, but I don't think that fixing this bug should wait for that. It may be borderline for being changed in a feature release (and definitely should not be changed in a maintenance release), but I think it would be OK. Mostly likely a program that uses spwd will also be using other root-only functions and already be producing other errors if run as a non-privileged user. The file-doesn't-exist case is a little more concerning, but my guess is that anyone actually checking for an empty return (as opposed to their program just failing when it gets one) would actually welcome the change, since it will give a more specific error message.

    Or, to put it another way, I think the negative impact of this in a feature release will be low enough that we won't take much if any flak for it :)

    @bitdancer bitdancer added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 10, 2014
    @giampaolo
    Copy link
    Contributor Author

    I still think this should be fixed (raise an exception) in the next major release.

    @bitdancer
    Copy link
    Member

    Unless Serhiy (or someone else) still objects, I say go ahead and commit it to default.

    @bitdancer
    Copy link
    Member

    Adding a test would be a good idea, though...

    @serhiy-storchaka
    Copy link
    Member

    This issue should be considered in conjunction with bpo-18787. And if we decise to break backward compatibility, this should be well documented in What's New.

    May be discuss these issues on Python-Dev?

    @pitrou
    Copy link
    Member

    pitrou commented Jan 1, 2015

    The approach in the patch seems good to me.

    @rbtcollins
    Copy link
    Member

    Too late for 3.5 for this IMO.

    @rbtcollins
    Copy link
    Member

    The man page for setspent says this:
    "The functions that return a pointer return NULL if no more entries are available or if an error occurs during processing. The functions which
    have int as the return value return 0 for success and -1 for failure, with errno set to indicate the cause of the error.

       For the nonreentrant functions, the return value may point to static area, and may be overwritten by subsequent calls to these functions.
    
       The reentrant functions return zero on success.  In case of error, an error number is returned.
    

    "

    -> that is, setspent's interaction with errno is undefined, at least on Linux. I'm a little worried about whether this may pickup false errors as a result.

    @rbtcollins
    Copy link
    Member

    I'm moving this back to patch review - it needs a test, particularly because of the question I have around setspent.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @hugovk
    Copy link
    Member

    hugovk commented Apr 12, 2022

    Note the spwd module is deprecated in 3.11 and set for removal in 3.13.

    See PEP 594 – Removing dead batteries from the standard library and #91217.

    @AA-Turner
    Copy link
    Member

    Closing per Hugo's comment.

    A

    @AA-Turner AA-Turner closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants