(uid, gid)
| 620 | |
| 621 | |
| 622 | def _setuid(uid, gid): |
| 623 | # If GID isn't defined, get the primary GID of the user. |
| 624 | if not gid and pwd: |
| 625 | gid = pwd.getpwuid(uid).pw_gid |
| 626 | # Must set the GID before initgroups(), as setgid() |
| 627 | # is known to zap the group list on some platforms. |
| 628 | |
| 629 | # setgid must happen before setuid (otherwise the setgid operation |
| 630 | # may fail because of insufficient privileges and possibly stay |
| 631 | # in a privileged group). |
| 632 | setgid(gid) |
| 633 | initgroups(uid, gid) |
| 634 | |
| 635 | # at last: |
| 636 | setuid(uid) |
| 637 | # ... and make sure privileges cannot be restored: |
| 638 | try: |
| 639 | setuid(0) |
| 640 | except OSError as exc: |
| 641 | if exc.errno != errno.EPERM: |
| 642 | raise |
| 643 | # we should get here: cannot restore privileges, |
| 644 | # everything was fine. |
| 645 | else: |
| 646 | raise SecurityError( |
| 647 | 'non-root user able to restore privileges after setuid.') |
| 648 | |
| 649 | |
| 650 | if hasattr(_signal, 'setitimer'): |
no test coverage detected