Skip to content

No longer need to run knockd with sudo #3472

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

Merged
merged 2 commits into from
May 31, 2025

Conversation

jasperchess
Copy link
Contributor

If you are hosting this image for unknown users, sudo access to knockd can be used for privilege escalation within the container.

There needs to be a lot of surrounding functionality for this to be a security risk, and for self-hosted minecraft servers this isn't a concern. I'm not 100% certain this is the correct approach, but I'm happy to make changes as necessary.

If you think this is the correct approach I'm happy to update (with the help of Peri123214) the docs outlining the risk once this has been patched.

Big thanks to Peri123214 for finding this.

@itzg
Copy link
Owner

itzg commented May 30, 2025

This approach looks fine. What I'm now wondering is with the setcap applied to the executable, maybe it doesn't even need to be sudo'ed

setcap cap_net_raw=ep /usr/local/sbin/knockd

Can you research that a bit?

@itzg
Copy link
Owner

itzg commented May 30, 2025

...in fact, I think I'll merge this now and we can follow up with the other research outcome.

scripts/start Outdated
@@ -48,6 +48,11 @@ if ! isTrue "${SKIP_SUDO:-false}" && [ "$(id -u)" = 0 ]; then
echo 'hosts: files dns' > /etc/nsswitch.conf
fi

if [[ ${ENABLE_AUTOPAUSE^^} == TRUE ]]; then
log "Autopause is enabled, setting up permissions"
echo "minecraft ALL=(ALL) NOPASSWD:/usr/local/sbin/knockd" >> /etc/sudoers.d/sudoers-mc
Copy link
Owner

Choose a reason for hiding this comment

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

I just realized, this will probably keep re-appending the row on startup when autopause is enabled.

@jasperchess
Copy link
Contributor Author

This approach looks fine. What I'm now wondering is with the setcap applied to the executable, maybe it doesn't even need to be sudo'ed

setcap cap_net_raw=ep /usr/local/sbin/knockd

Can you research that a bit?

Ah good point! Let me look into that. If it is still required to sudo I'll fix the constant appending on every start - good catch!

@jasperchess
Copy link
Contributor Author

@itzg you were correct, with the capabilities set, it isn't necessary to sudo start knockd.

The testing I've done:

  1. Start the server -e ENABLE_AUTOPAUSE=TRUE -e AUTOPAUSE_TIMEOUT_INIT=30 (without adding --add-cap to the start command)
  2. Wait for the autostop functionality to kick in
  3. Try and connect

I haven't tested every distro (alpine/ubuntu/ol), let me know if more thorough testing is necessary

@itzg
Copy link
Owner

itzg commented May 31, 2025

Awesome! I'm not sure why that didn't dawn on me when the setcap change was first contributed, but glad to leverage it now. 😃

That sounds like sufficient testing and can follow up with other distros later if needed.

Copy link
Owner

@itzg itzg left a comment

Choose a reason for hiding this comment

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

Thanks!

@jasperchess
Copy link
Contributor Author

Thanks for the quick reviews!

@itzg itzg merged commit e6cd711 into itzg:master May 31, 2025
8 of 9 checks passed
@itzg itzg changed the title patch: conditionally add knockd to sudoers file No longer need to run knockd with sudo Jun 1, 2025
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.

2 participants