Skip to content

chown: preserve non-UTF-8 account names - #15502

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/chown-non-utf8-account-names-9958
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/chown-non-utf8-account-names-9958

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

Preserve account-name bytes through owner and --from parsing and account lookups, including the login-group shorthand. Test successful ownership changes with synthetic non-UTF-8 accounts.

Closes #9958.

@limadog9
limadog9 force-pushed the fix/chown-non-utf8-account-names-9958 branch from 5c8b168 to a6e4746 Compare October 18, 2026 19:59
@sylvestre

Copy link
Copy Markdown
Contributor

Mostly the test is left: please make it simpler and make it actually check that --from matched.

Comment thread src/uucore/src/lib/features/perms.rs Outdated
let mut owner_arg = Arg::new(add_arg_if_not_reference)
.value_name(add_arg_if_not_reference)
.required(true);
if !groups_only {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not for chgrp too?

Comment thread tests/by-util/test_chown.rs Outdated

let ts = TestScenario::new(util_name!());
let library = ts.fixtures.plus("accounts.so");
let compilation = Command::new("cc")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compiling a C shim with cc in a test is a lot of machinery, do we really need it?
maybe we can just check that the error is now "invalid user" and not the clap UTF-8 error

Comment thread tests/by-util/test_chown.rs Outdated
}
ts.ucmd()
.env("LD_PRELOAD", &library)
.arg("--from")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ids map to our own, so this passes even if --from never matches
could you please use -v and check the "retained"/"changed" output?

Comment thread tests/by-util/test_chown.rs Outdated
.arg("file")
.succeeds();
assert_eq!(ts.fixtures.metadata("file").uid(), unsafe {
libc::geteuid()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe without SAFETY, and the same metadata is read twice, could be simpler

@limadog9
limadog9 force-pushed the fix/chown-non-utf8-account-names-9958 branch from a6e4746 to cfa4fbf Compare October 18, 2026 22:35
Comment thread src/uu/chown/src/chown.rs Outdated
/// this to `':'`. If a separator is given but the group is omitted
/// (as in `"owner:"`), the group is set to the owner's login group.
fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
fn parse_spec(spec: impl AsRef<OsStr>, sep: char) -> UResult<(Option<u32>, Option<u32>)> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why impl AsRef<OsStr>? &OsStr should be enough

Comment thread tests/by-util/test_chgrp.rs Outdated
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

for from in [false, true] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two plain new_ucmd!() calls would be easier to read than this loop with if from three times

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for tackling this!
Almost there: the chgrp test could be simpler, and chown --from needs a non-UTF-8 case (details inline).

use std::os::unix::ffi::OsStrExt;
for (spec, message) in [
(b"unknown_\xff".as_slice(), "invalid user:"),
(b":unknown_\xff".as_slice(), "invalid group:"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a --from case with a non-UTF-8 value too?

Preserve account-name bytes through owner and --from parsing and account lookups, including the login-group shorthand. Test successful ownership changes with synthetic non-UTF-8 accounts.

Closes uutils#9958.
@limadog9
limadog9 force-pushed the fix/chown-non-utf8-account-names-9958 branch from cfa4fbf to 69165ea Compare October 19, 2026 19:28

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chown cannot change ownership to a user with usernames that are not UTF-8

2 participants