@@ -4400,6 +4400,14 @@ static PyObject *
44004400os_chroot_impl(PyObject *module, path_t *path)
44014401/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
44024402{
4403+ #ifdef __ANDROID__
4404+ // On Android, calling this function as a non-root user leads to a process crash
4405+ // rather than returning a permission error.
4406+ if (getuid() != 0) {
4407+ errno = EPERM;
4408+ return path_error(path);
4409+ }
4410+ #endif
44034411 int res;
44044412 Py_BEGIN_ALLOW_THREADS
44054413 res = chroot(path->narrow);
@@ -9855,6 +9863,14 @@ os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
98559863/*[clinic end generated code: output=59341244521a9e3f input=7e4514dff4526a95]*/
98569864#endif
98579865{
9866+ #ifdef __ANDROID__
9867+ // On Android, calling this function as a non-root user leads to a process crash
9868+ // rather than returning a permission error.
9869+ if (getuid() != 0) {
9870+ errno = EPERM;
9871+ return PyErr_SetFromErrno(PyExc_OSError);
9872+ }
9873+ #endif
98589874 const char *username = PyBytes_AS_STRING(oname);
98599875
98609876 if (initgroups(username, gid) == -1)
@@ -10278,6 +10294,14 @@ static PyObject *
1027810294os_setuid_impl(PyObject *module, uid_t uid)
1027910295/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
1028010296{
10297+ #ifdef __ANDROID__
10298+ // On Android, calling this function as a non-root user leads to a process crash
10299+ // rather than returning a permission error.
10300+ if (getuid() != 0) {
10301+ errno = EPERM;
10302+ return posix_error();
10303+ }
10304+ #endif
1028110305 if (setuid(uid) < 0)
1028210306 return posix_error();
1028310307 Py_RETURN_NONE;
@@ -10299,6 +10323,14 @@ static PyObject *
1029910323os_seteuid_impl(PyObject *module, uid_t euid)
1030010324/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
1030110325{
10326+ #ifdef __ANDROID__
10327+ // On Android, calling this function as a non-root user leads to a process crash
10328+ // rather than returning a permission error.
10329+ if (getuid() != 0) {
10330+ errno = EPERM;
10331+ return posix_error();
10332+ }
10333+ #endif
1030210334 if (seteuid(euid) < 0)
1030310335 return posix_error();
1030410336 Py_RETURN_NONE;
@@ -10320,6 +10352,14 @@ static PyObject *
1032010352os_setegid_impl(PyObject *module, gid_t egid)
1032110353/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
1032210354{
10355+ #ifdef __ANDROID__
10356+ // On Android, calling this function as a non-root user leads to a process crash
10357+ // rather than returning a permission error.
10358+ if (getuid() != 0) {
10359+ errno = EPERM;
10360+ return posix_error();
10361+ }
10362+ #endif
1032310363 if (setegid(egid) < 0)
1032410364 return posix_error();
1032510365 Py_RETURN_NONE;
@@ -10342,6 +10382,14 @@ static PyObject *
1034210382os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
1034310383/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
1034410384{
10385+ #ifdef __ANDROID__
10386+ // On Android, calling this function as a non-root user leads to a process crash
10387+ // rather than returning a permission error.
10388+ if (getuid() != 0) {
10389+ errno = EPERM;
10390+ return posix_error();
10391+ }
10392+ #endif
1034510393 if (setreuid(ruid, euid) < 0) {
1034610394 return posix_error();
1034710395 } else {
@@ -10366,6 +10414,14 @@ static PyObject *
1036610414os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
1036710415/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
1036810416{
10417+ #ifdef __ANDROID__
10418+ // On Android, calling this function as a non-root user leads to a process crash
10419+ // rather than returning a permission error.
10420+ if (getuid() != 0) {
10421+ errno = EPERM;
10422+ return posix_error();
10423+ }
10424+ #endif
1036910425 if (setregid(rgid, egid) < 0)
1037010426 return posix_error();
1037110427 Py_RETURN_NONE;
@@ -10386,6 +10442,14 @@ static PyObject *
1038610442os_setgid_impl(PyObject *module, gid_t gid)
1038710443/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
1038810444{
10445+ #ifdef __ANDROID__
10446+ // On Android, calling this function as a non-root user leads to a process crash
10447+ // rather than returning a permission error.
10448+ if (getuid() != 0) {
10449+ errno = EPERM;
10450+ return posix_error();
10451+ }
10452+ #endif
1038910453 if (setgid(gid) < 0)
1039010454 return posix_error();
1039110455 Py_RETURN_NONE;
@@ -15390,6 +15454,14 @@ static PyObject *
1539015454os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
1539115455/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
1539215456{
15457+ #ifdef __ANDROID__
15458+ // On Android, calling this function as a non-root user leads to a process crash
15459+ // rather than returning a permission error.
15460+ if (getuid() != 0) {
15461+ errno = EPERM;
15462+ return posix_error();
15463+ }
15464+ #endif
1539315465 if (setresuid(ruid, euid, suid) < 0)
1539415466 return posix_error();
1539515467 Py_RETURN_NONE;
@@ -15413,6 +15485,14 @@ static PyObject *
1541315485os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
1541415486/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
1541515487{
15488+ #ifdef __ANDROID__
15489+ // On Android, calling this function as a non-root user leads to a process crash
15490+ // rather than returning a permission error.
15491+ if (getuid() != 0) {
15492+ errno = EPERM;
15493+ return posix_error();
15494+ }
15495+ #endif
1541615496 if (setresgid(rgid, egid, sgid) < 0)
1541715497 return posix_error();
1541815498 Py_RETURN_NONE;
0 commit comments