Skip to content

Commit ad706e0

Browse files
committed
pp_aassign: move UID/GID code into separate fn
There is a big chunk of code near the end of pp_aassign() which is responsible for updating the OS's UIDs and/or GIDs after something like ($<,$>) = ($>,$<); has been executed. This code is long and has little to with list assignment generally, so move it out into a separate static function. This commit makes no changes to that chunk of code apart from indentation (hopefully).
1 parent c4e9fef commit ad706e0

File tree

1 file changed

+98
-83
lines changed

1 file changed

+98
-83
lines changed

pp_hot.c

Lines changed: 98 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,6 +2556,101 @@ S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem,
25562556
}
25572557

25582558

2559+
/* Helper function for pp_aassign(): after performing something like
2560+
*
2561+
* ($<,$>) = ($>,$<); # swap real and effective uids
2562+
*
2563+
* the assignment to the magic variables just sets various flags in
2564+
* PL_delaymagic; now we tell the OS to update the uids/gids atomically.
2565+
*/
2566+
2567+
STATIC void
2568+
S_aassign_uid(pTHX)
2569+
{
2570+
/* Will be used to set PL_tainting below */
2571+
Uid_t tmp_uid = PerlProc_getuid();
2572+
Uid_t tmp_euid = PerlProc_geteuid();
2573+
Gid_t tmp_gid = PerlProc_getgid();
2574+
Gid_t tmp_egid = PerlProc_getegid();
2575+
2576+
/* XXX $> et al currently silently ignore failures */
2577+
if (PL_delaymagic & DM_UID) {
2578+
#ifdef HAS_SETRESUID
2579+
PERL_UNUSED_RESULT(
2580+
setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
2581+
(PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
2582+
(Uid_t)-1));
2583+
#elif defined(HAS_SETREUID)
2584+
PERL_UNUSED_RESULT(
2585+
setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
2586+
(PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
2587+
#else
2588+
# ifdef HAS_SETRUID
2589+
if ((PL_delaymagic & DM_UID) == DM_RUID) {
2590+
PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
2591+
PL_delaymagic &= ~DM_RUID;
2592+
}
2593+
# endif /* HAS_SETRUID */
2594+
# ifdef HAS_SETEUID
2595+
if ((PL_delaymagic & DM_UID) == DM_EUID) {
2596+
PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
2597+
PL_delaymagic &= ~DM_EUID;
2598+
}
2599+
# endif /* HAS_SETEUID */
2600+
if (PL_delaymagic & DM_UID) {
2601+
if (PL_delaymagic_uid != PL_delaymagic_euid)
2602+
DIE(aTHX_ "No setreuid available");
2603+
PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
2604+
}
2605+
#endif /* HAS_SETRESUID */
2606+
2607+
tmp_uid = PerlProc_getuid();
2608+
tmp_euid = PerlProc_geteuid();
2609+
}
2610+
2611+
/* XXX $> et al currently silently ignore failures */
2612+
if (PL_delaymagic & DM_GID) {
2613+
#ifdef HAS_SETRESGID
2614+
PERL_UNUSED_RESULT(
2615+
setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
2616+
(PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
2617+
(Gid_t)-1));
2618+
#elif defined(HAS_SETREGID)
2619+
PERL_UNUSED_RESULT(
2620+
setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
2621+
(PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
2622+
#else
2623+
# ifdef HAS_SETRGID
2624+
if ((PL_delaymagic & DM_GID) == DM_RGID) {
2625+
PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
2626+
PL_delaymagic &= ~DM_RGID;
2627+
}
2628+
# endif /* HAS_SETRGID */
2629+
# ifdef HAS_SETEGID
2630+
if ((PL_delaymagic & DM_GID) == DM_EGID) {
2631+
PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
2632+
PL_delaymagic &= ~DM_EGID;
2633+
}
2634+
# endif /* HAS_SETEGID */
2635+
if (PL_delaymagic & DM_GID) {
2636+
if (PL_delaymagic_gid != PL_delaymagic_egid)
2637+
DIE(aTHX_ "No setregid available");
2638+
PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
2639+
}
2640+
#endif /* HAS_SETRESGID */
2641+
2642+
tmp_gid = PerlProc_getgid();
2643+
tmp_egid = PerlProc_getegid();
2644+
}
2645+
TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
2646+
#ifdef NO_TAINT_SUPPORT
2647+
PERL_UNUSED_VAR(tmp_uid);
2648+
PERL_UNUSED_VAR(tmp_euid);
2649+
PERL_UNUSED_VAR(tmp_gid);
2650+
PERL_UNUSED_VAR(tmp_egid);
2651+
#endif
2652+
}
2653+
25592654

25602655
PP(pp_aassign)
25612656
{
@@ -3316,89 +3411,9 @@ PP(pp_aassign)
33163411

33173412
TAINT_NOT; /* result of list assign isn't tainted */
33183413

3319-
if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) {
3320-
/* Will be used to set PL_tainting below */
3321-
Uid_t tmp_uid = PerlProc_getuid();
3322-
Uid_t tmp_euid = PerlProc_geteuid();
3323-
Gid_t tmp_gid = PerlProc_getgid();
3324-
Gid_t tmp_egid = PerlProc_getegid();
3325-
3326-
/* XXX $> et al currently silently ignore failures */
3327-
if (PL_delaymagic & DM_UID) {
3328-
#ifdef HAS_SETRESUID
3329-
PERL_UNUSED_RESULT(
3330-
setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
3331-
(PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
3332-
(Uid_t)-1));
3333-
#elif defined(HAS_SETREUID)
3334-
PERL_UNUSED_RESULT(
3335-
setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
3336-
(PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
3337-
#else
3338-
# ifdef HAS_SETRUID
3339-
if ((PL_delaymagic & DM_UID) == DM_RUID) {
3340-
PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
3341-
PL_delaymagic &= ~DM_RUID;
3342-
}
3343-
# endif /* HAS_SETRUID */
3344-
# ifdef HAS_SETEUID
3345-
if ((PL_delaymagic & DM_UID) == DM_EUID) {
3346-
PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
3347-
PL_delaymagic &= ~DM_EUID;
3348-
}
3349-
# endif /* HAS_SETEUID */
3350-
if (PL_delaymagic & DM_UID) {
3351-
if (PL_delaymagic_uid != PL_delaymagic_euid)
3352-
DIE(aTHX_ "No setreuid available");
3353-
PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
3354-
}
3355-
#endif /* HAS_SETRESUID */
3356-
3357-
tmp_uid = PerlProc_getuid();
3358-
tmp_euid = PerlProc_geteuid();
3359-
}
3360-
/* XXX $> et al currently silently ignore failures */
3361-
if (PL_delaymagic & DM_GID) {
3362-
#ifdef HAS_SETRESGID
3363-
PERL_UNUSED_RESULT(
3364-
setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
3365-
(PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
3366-
(Gid_t)-1));
3367-
#elif defined(HAS_SETREGID)
3368-
PERL_UNUSED_RESULT(
3369-
setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
3370-
(PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
3371-
#else
3372-
# ifdef HAS_SETRGID
3373-
if ((PL_delaymagic & DM_GID) == DM_RGID) {
3374-
PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
3375-
PL_delaymagic &= ~DM_RGID;
3376-
}
3377-
# endif /* HAS_SETRGID */
3378-
# ifdef HAS_SETEGID
3379-
if ((PL_delaymagic & DM_GID) == DM_EGID) {
3380-
PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
3381-
PL_delaymagic &= ~DM_EGID;
3382-
}
3383-
# endif /* HAS_SETEGID */
3384-
if (PL_delaymagic & DM_GID) {
3385-
if (PL_delaymagic_gid != PL_delaymagic_egid)
3386-
DIE(aTHX_ "No setregid available");
3387-
PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
3388-
}
3389-
#endif /* HAS_SETRESGID */
3390-
3391-
tmp_gid = PerlProc_getgid();
3392-
tmp_egid = PerlProc_getegid();
3393-
}
3394-
TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
3395-
#ifdef NO_TAINT_SUPPORT
3396-
PERL_UNUSED_VAR(tmp_uid);
3397-
PERL_UNUSED_VAR(tmp_euid);
3398-
PERL_UNUSED_VAR(tmp_gid);
3399-
PERL_UNUSED_VAR(tmp_egid);
3400-
#endif
3401-
}
3414+
if (UNLIKELY(PL_delaymagic & ~DM_DELAY))
3415+
/* update system UIDs and/or GIDs */
3416+
S_aassign_uid(aTHX);
34023417
PL_delaymagic = old_delaymagic;
34033418

34043419
#ifdef PERL_RC_STACK

0 commit comments

Comments
 (0)