On Thu, Apr 28, 2022 at 11:02 AM Phillip Wood <phillip.wood123@xxxxxxxxx> wrote: > > On 28/04/2022 11:58, Carlo Marcelo Arenas Belón wrote: > > I still don't understand why you're worried about preserving errno - am > I missing something? I don't think so, you are correct that doing so is useless now and could be dropped, I even said so in my message. > It's not wrong to save it but I'm not sure why we > need to. is_path_owned_by_current_uid() does not save it around the stat > call so why do we need to do this here? because the errno from is_path_owned_by_current_uid() is useful and unlike this one is not being handled, so a caller of that function might later use it to distinguish between a "dunno" and "no" answer to the "is path owned by current uid" question and more importantly understand better why "dunno" was the answer (ex: path has a loop, or not accessible or whatever). it also documents (in the code) that this function won't report any issues it might have getting that information from the environment (which extra checks on top to make sure anything suspicious is ignored), so you can trust that if you get an answer it was a valid one. > > + errno = 0; > > + env_id = strtoul(real_uid, &endptr, 10); > > + if (!errno && !*endptr && env_id <= (uid_t)-1) > > Thinking out loud: > "!errno && !*endptr" ensures we have read a valid integer from SUDO_UID. > If uid_t is unsigned then "env_id <= (uid_t)-1" ensures the value we > read fits into a uid_t. If uid_t is signed then this test is always true and you hopefully got a warning at build time. > and we could truncate the value we read. However if we don't trust > SUDO_UID then we shouldn't be doing any of this so we are trusting that > the truncation never happens which means we probably don't need this > test in the first place. it is there to protect us against a system where uid_t is unsigned but still narrower than long, and so we don't accidentally assume uid = 0 if we somehow get (the obviously tampered with) SUDO_UID = UINT_MAX + 1 Carlo