Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user: simplify ssh key provisioning and drop unsafe usage #95

Merged
merged 1 commit into from
Jul 3, 2024

Conversation

jeremycline
Copy link
Member

In order to correctly set the ownership of the authorized_keys file, the function was using libc directly with some unsafe blocks. It's tricky to correctly use the C APIs and in this case, both getpwnam() and getgrnam() return NULL if a matching entry isn't found or if an error occurred. Since we weren't checking the return value this would result in a null pointer dereference.

nix provides safe APIs to retrieve the user and group ids so rather than implementing it ourselves, just use those. This also refactors the two separate APIs for creating the directory and writing the keys to a single call that handles it all.

This is extracted from #91 as it's not really related to the API design.

Copy link
Contributor

@anhvoms anhvoms left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you!

Copy link
Collaborator

@dongsupark dongsupark left a comment

Choose a reason for hiding this comment

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

Looks good in general.

let new_uid = Uid::from_raw(uid);

let gid_groupname = CString::new(username)?;
let gid_group = unsafe { libc::getgrnam(gid_groupname.as_ptr()) };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Removing the libc-related code like that, it becomes now unnecessary to keep libc any more.
Can you please remove libc in libazureinit/Cargo.toml?

To catch such issues, we should actually regularly run cargo machete or so. And there are other unused crates in this repo, but I will soon create another PR to remove those.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, I've dropped it

In order to correctly set the ownership of the authorized_keys file, the
function was using libc directly with some unsafe blocks. It's tricky to
correctly use the C APIs and in this case, both getpwnam() and getgrnam()
return NULL if a matching entry isn't found or if an error occurred.
Since we weren't checking the return value this would result in a null
pointer dereference.

`nix` provides safe APIs to retrieve the user and group ids so rather
than implementing it ourselves, just use those. This also refactors the
two separate APIs for creating the directory and writing the keys to a
single call that handles it all.
@jeremycline jeremycline merged commit 773f4cf into Azure:main Jul 3, 2024
5 checks passed
@jeremycline jeremycline deleted the jcline-drop-unsafe-ssh branch July 3, 2024 13:57
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.

None yet

3 participants