[Toybox] [landley/toybox] « sed -i (edit in place) » used as root doesn't preserve uid nor gid (#52)

Rob Landley rob at landley.net
Fri Sep 30 15:56:50 PDT 2016


On 09/30/2016 05:02 AM, ed38220 wrote:
> Hi!
> 
> Samsung Galaxy S4 Mini Plus I9195I (CM 13 unofficial)
> toybox version : 0.7.0-892c602b10e7-android
> 
> |# ls -l|
> |-rw-rw---- 1 u0_a201 u0_a201 4 2016-09-30 11:35 bb_test.txt|
> |-rw-rw---- 1 u0_a201 u0_a201 4 2016-09-30 11:35 tb_test.txt|
> 
> |# sed -i s/foo/bar/ tb_test.txt (toybox)|
> |# /system/xbin/sed -i s/foo/bar/ bb_test.txt (busybox)|
> 
> |# ls -l|
> |-rw-rw---- 1 u0_a201 u0_a201 4 2016-09-30 11:39 bb_test.txt (busybox)|
> |-rw-rw---- 1 root root 4 2016-09-30 11:39 tb_test.txt (toybox)|

Ah! We were doing fchmod() (in lib/lib.c copy_tempfile()) but not
fchown(). So preserving permissions but not ownership.

And the gcc developers went even crazier, so now typecasting to (void)
won't suppress the "unused result" warning. But an if() with no body does.

Yes this call fails if we're not root, and we expect and ignore that. If
it fails when we _are_ root what exactly are we supposed to _do_ about
it? We're already doing the fd operation instead of the directory
operation to eliminate the create/delete/recreate race, if the fchown()
fails on an already opened filehandle it's either because our filesystem
got unmounted under us (in which case we can't write the file contents)
or else SELinux did something crazy (again) and you get to keep the pieces.

There's an old saying about applied computer science involving finding
the right wrench to pound in the correct screw. Any "are you sure you
want to do that" warning that does not take "yes" for an answer will be
fixed with hot irons, then publicly mocked.

Rob


More information about the Toybox mailing list