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

python/pysss_nss_idmap: check return from functions #5226

Closed
wants to merge 4 commits into from

Conversation

ikerexxe
Copy link
Contributor

@ikerexxe ikerexxe commented Jul 1, 2020

Coverity warns that PyModule_AddIntConstant() returns operation success
or failure but this value is never checked.

Error: CHECKED_RETURN (CWE-252):
sssd-2.3.0/src/python/pysss_nss_idmap.c:587: check_return: Calling
"PyModule_AddIntConstant" without checking return value (as is done
elsewhere 4 out of 5 times).
sssd-2.3.0/src/python/pyhbac.c:1956: example_assign: Example 1:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_CATEGORY_ALL", 1L)".
sssd-2.3.0/src/python/pyhbac.c:1957: example_checked: Example 1 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1960: example_assign: Example 2:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_RULE_ELEMENT_USERS", 1L)".
sssd-2.3.0/src/python/pyhbac.c:1961: example_checked: Example 2 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1972: example_assign: Example 3:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_EVAL_DENY", HBAC_EVAL_DENY)".
sssd-2.3.0/src/python/pyhbac.c:1973: example_checked: Example 3 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1982: example_assign: Example 4:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_ERROR_NOT_IMPLEMENTED", HBAC_ERROR_NOT_IMPLEMENTED)".
sssd-2.3.0/src/python/pyhbac.c:1983: example_checked: Example 4 (cont.):
"ret" has its value checked in "ret == -1".
 #  585|       PyModule_AddIntConstant(module, "ID_NOT_SPECIFIED",
 #  586|                               SSS_ID_TYPE_NOT_SPECIFIED);
 #  587|->     PyModule_AddIntConstant(module, "ID_USER", SSS_ID_TYPE_UID);
 #  588|       PyModule_AddIntConstant(module, "ID_GROUP", SSS_ID_TYPE_GID);
 #  589|       PyModule_AddIntConstant(module, "ID_BOTH", SSS_ID_TYPE_BOTH);

Moreover, even though coverity doesn't indicate it the same happens with
PyModule_AddStringConstant().

src/python/pyhbac.c Outdated Show resolved Hide resolved
@alexey-tikhonov
Copy link
Member

Thanks for the update. Please see comment inline.

Btw, wouldn't it make sense to include Py_DECREF(module); (actually better Py_XDECREF) into MODINITERROR?

@ikerexxe
Copy link
Contributor Author

ikerexxe commented Sep 8, 2020

Thanks for the update. Please see comment inline.

Yes, it makes sense. I'll change it.

Btw, wouldn't it make sense to include Py_DECREF(module); (actually better Py_XDECREF) into MODINITERROR?

It makes sense to include Py_XDECREF(module) in another macro called FREEMOD_AND_RETURN(?) and call it from the changes I've made. I wouldn't like messing with MODINITERROR as it's already called from other places where it doesn't make to decrease module. An example:

MODINITERROR; \

What do you think?

@alexey-tikhonov
Copy link
Member

I think you just spotted yet another one place that needs fixing: TYPE_READY calls PyModule_AddObject() and doesn't check a result.

Moreover, taking into account where this macro (TYPE_READY) is used, Py_XDECREF should be called there as well, IMO.

@ikerexxe
Copy link
Contributor Author

ikerexxe commented Sep 9, 2020

I think you just spotted yet another one place that needs fixing: TYPE_READY calls PyModule_AddObject() and doesn't check a result.

Moreover, taking into account where this macro (TYPE_READY) is used, Py_XDECREF should be called there as well, IMO.

Agreed. Take a look to the latest update, which contains all the aforementioned errors fixed.

@alexey-tikhonov
Copy link
Member

Why do we need both MODINITERROR and FREEMOD_AND_RETURN?

I still think we can have single macro (calling Py_XDECREF on NULL should be safe) and use it everywhere.

@alexey-tikhonov
Copy link
Member

Thank you for the updated version. Please see a couple of comments inline.

@ikerexxe ikerexxe closed this Sep 16, 2020
@ikerexxe ikerexxe deleted the check_return branch September 16, 2020 13:56
@ikerexxe ikerexxe reopened this Sep 16, 2020
Change MODINITERROR macro to dereference module when PyModule_*
interfaces report some type of failure.
Coverity warns that PyModule_AddIntConstant() returns operation success
or failure but this value is never checked.

```
Error: CHECKED_RETURN (CWE-252):
sssd-2.3.0/src/python/pysss_nss_idmap.c:587: check_return: Calling
"PyModule_AddIntConstant" without checking return value (as is done
elsewhere 4 out of 5 times).
sssd-2.3.0/src/python/pyhbac.c:1956: example_assign: Example 1:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_CATEGORY_ALL", 1L)".
sssd-2.3.0/src/python/pyhbac.c:1957: example_checked: Example 1 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1960: example_assign: Example 2:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_RULE_ELEMENT_USERS", 1L)".
sssd-2.3.0/src/python/pyhbac.c:1961: example_checked: Example 2 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1972: example_assign: Example 3:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_EVAL_DENY", HBAC_EVAL_DENY)".
sssd-2.3.0/src/python/pyhbac.c:1973: example_checked: Example 3 (cont.):
"ret" has its value checked in "ret == -1".
sssd-2.3.0/src/python/pyhbac.c:1982: example_assign: Example 4:
Assigning: "ret" = return value from "PyModule_AddIntConstant(m,
"HBAC_ERROR_NOT_IMPLEMENTED", HBAC_ERROR_NOT_IMPLEMENTED)".
sssd-2.3.0/src/python/pyhbac.c:1983: example_checked: Example 4 (cont.):
"ret" has its value checked in "ret == -1".
 #  585|       PyModule_AddIntConstant(module, "ID_NOT_SPECIFIED",
 #  586|                               SSS_ID_TYPE_NOT_SPECIFIED);
 #  587|->     PyModule_AddIntConstant(module, "ID_USER", SSS_ID_TYPE_UID);
 #  588|       PyModule_AddIntConstant(module, "ID_GROUP", SSS_ID_TYPE_GID);
 #  589|       PyModule_AddIntConstant(module, "ID_BOTH", SSS_ID_TYPE_BOTH);
```

Moreover, even though coverity doesn't indicate it the same happens with
PyModule_AddStringConstant().
If PyModule* actions fail, then references to objects have to be
decremented.
If PyModule_AddObject fails, then references to objects have to be
decremented.
@alexey-tikhonov
Copy link
Member

Thank you. ACK.

@pbrezina
Copy link
Member

Pushed PR: #5226

  • master
    • 03b00f7 - python/pysss: if PyModule* fails decrement references
    • 8b1a8cf - python/pyhbac: if PyModule* fails decrement references
    • c008d89 - python/pysss_nss_idmap: check return from functions
    • 838baa8 - util/sss_python: change MODINITERROR to dereference module

@pbrezina pbrezina added Pushed and removed Accepted Ready to push Ready to push labels Sep 17, 2020
@pbrezina pbrezina closed this Sep 17, 2020
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

3 participants