(uid, gid)
| 556 | |
| 557 | |
| 558 | def _setuid(uid, gid): |
| 559 | # If GID isn't defined, get the primary GID of the user. |
| 560 | if not gid and pwd: |
| 561 | gid = pwd.getpwuid(uid).pw_gid |
| 562 | # Must set the GID before initgroups(), as setgid() |
| 563 | # is known to zap the group list on some platforms. |
| 564 | |
| 565 | # setgid must happen before setuid (otherwise the setgid operation |
| 566 | # may fail because of insufficient privileges and possibly stay |
| 567 | # in a privileged group). |
| 568 | setgid(gid) |
| 569 | initgroups(uid, gid) |
| 570 | |
| 571 | # at last: |
| 572 | setuid(uid) |
| 573 | # ... and make sure privileges cannot be restored: |
| 574 | try: |
| 575 | setuid(0) |
| 576 | except OSError as exc: |
| 577 | if exc.errno != errno.EPERM: |
| 578 | raise |
| 579 | # we should get here: cannot restore privileges, |
| 580 | # everything was fine. |
| 581 | else: |
| 582 | raise SecurityError( |
| 583 | 'non-root user able to restore privileges after setuid.') |
| 584 | |
| 585 | |
| 586 | if hasattr(_signal, 'setitimer'): |
no test coverage detected