cloud-init enables ssh password auth in an unexpected config file

Bug #2088207 reported by Mark Esler
260
This bug affects 2 people
Affects Status Importance Assigned to Milestone
cloud-init
New
Unknown
cloud-init (Ubuntu)
Invalid
Undecided
Unassigned
openssh (Ubuntu)
Fix Released
Medium
Nick Rosbrook

Bug Description

Last night <email address hidden> received a security report about cloud-init:
```
Hello

Most server admins are familiar with disabling password auth in /etc/ssh/sshd_config.
However Ubuntu Server 24.04 when installed from the ISO (https://ubuntu.com/download/server)
 includes a new file `/etc/ssh/sshd_config.d/50-cloud-init.conf`.

This means that disabling password auth in `/etc/ssh/sshd_config` does nothing:

    # To disable tunneled clear text passwords, change to no here!
    PasswordAuthentication no
    ^ Setting it to "no" does nothing

Server admins also need to delete `/etc/ssh/sshd_config.d/50-cloud-init.conf` which contains a single line:

        PasswordAuthentication yes

There is no documentation for server admins that this is necessary in /etc/ssh/sshd_config nor is this expected and will cause massive security problems as upgrade in the future. People are just discovering this behaviour now:

[0] https://www.mikeberggren.com/deb-ssh-auth
[1] https://askubuntu.com/questions/1516262/why-is-50-cloud-init-conf-created
[2] https://askubuntu.com/a/435620

Recommendation:
1. Don't include this file by default
2. OR update sshd_config documentation so people know to check /etc/ssh/ssd_config.d/

lllf
```

@falcojr from cloud-init added that:
> this happens due to the subiquity installer setting passwordauthentication yes by default
> cloud-init writes any explicit configuration about ssh into sshd_config.d

To summarize:
Often `PasswordAuthentication` is disabled in `/etc/ssh/sshd_config`. When cloud-init is used, this value is set in `/etc/ssh/sshd_config.d/50-cloud-init.conf` and will override `/etc/ssh/sshd_config`. If an admin is not aware of this additional config file or how sshd loads configs, they may unintentionally allow PasswordAuthentication.

My inclination is to opt for lllf's second recommendation and clearly document the additional config file. Possibly the header of /etc/ssh/sshd_config could include:
```
# Note that cloud-init has generated /etc/ssh/sshd_config.d/50-cloud-init.conf
# configurations in sshd_config.d may override settings in this file
# such as overriding PasswordAuthentication to yes
```

Related branches

Mark Esler (eslerm)
description: updated
Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

Is there a reason cloud-init needs to create an override in the first place, rather than changing the setting in the main file?

Revision history for this message
Mark Esler (eslerm) wrote :

Cheers to @jchittum for pointing out that ssh clearly documents sshd_config.d

https://manpages.ubuntu.com/manpages/noble/en/man5/sshd_config.5.html

> /etc/ssh/sshd_config.d/*.conf files are included at the start of the configuration file, so
> options set there will override those in /etc/ssh/sshd_config.

If we continue to use sshd_config.d, how can we help raise awareness?

Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

While the override directory is documented, it is quite unexpected that a default installation will make use of it, which is why this bug exists in the first place.

Revision history for this message
Mark Esler (eslerm) wrote :

When using the latest 24.04 server installer, it will ask if you want to opt in to ssh. On opt in, the installer selects "Allow password authentication over SSH" automatically, and can only be turned off if the user provides a key.

So, between sshd_config (5) and subiquity's documentation, I no longer believe this is wholly unexpected. We should still work to resolve this. I like Marc's suggestion of avoiding sshd_config.d all together. If we keep sshd_config.d, a warning in /etc/ssh/sshd_config would be nice.

(Are there other creative solutions to this? sshd_config's conditional Match blocks don't seem to help here.)

Revision history for this message
James Falcon (falcojr) wrote :

> avoiding sshd_config.d all together

Why? Isn't this the entire point of the .d directory? The package provides a default configuration. If another package or service (like cloud-init) wants to change it, they should be dropping in an override file. If cloud-init changes the config file it's less obvious that cloud-init made the change and upgrades are trickier because there are changed values that the user needs to deal with that they never explicitly set.

Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

Perhaps an acceptable solution could be to write the file only if cloud-init needs to overwrite the value to "no", but if the value is "yes", the openssh default, it shouldn't create the file. This would allow continuing to use the .d directory, but would prevent confusion which results in password authentication being enabled contrary to expectations.

Revision history for this message
Mark Esler (eslerm) wrote :

> > avoiding sshd_config.d all together

> Why?

You have convinced me that sshd_config.d is the most technically advantageous and correct way to configure ssh. It is not intuitive though. That is why I am for lllf's second recommendation.

I like Marc's suggestion in addition to lllf's. We could implement both together. Similar to what Marc suggests, we could instead change subiquity to only set PasswordAuthentication when the value is No.

Revision history for this message
Brett Holman (holmanb) wrote :

> Perhaps an acceptable solution could be to write the file only if cloud-init needs to overwrite the value to "no", but if the value is "yes", the openssh default, it shouldn't create the file.

This would require cloud-init to implement a parser which correctly parses all of sshd's configuration files (sshd_config and sshd_config.d) with exactly the same semantics in order to identify the current value. Sounds easy to implement something that would appear to work, but this suggestion sounds error prone and full of edge cases and risk of implementation drift between sshd and cloud-init. It seems to me that the problem is that users expectations don't align with documented behaviors. I think it would be less risky to fix a user expectations problem with docs / comments rather than potentially introducing vulnerabilities in the process of managing user's perceptions.

Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

If you google "how to disable ssh password authentication", there are pages and pages of instructions that instruct to modify sshd_config. I'm not sure how to correct user expectations. Maybe adding more explicit comments to sshd_config could be okay.

How is cloud-init making sure another file in sshd_config.d isn't superseding its config?

Revision history for this message
Seth Arnold (seth-arnold) wrote :

Brett, I think Marc's 'only if cloud-init needs to overwrite the value to "no"' was less about the existing sshd configuration (afterall, if cloud-init is running, did the configuration have meaningful values ten seconds earlier?) and more about the user-data being explicit that passwords should be used in preference to ssh keys for logging in.

Certainly re-implementing sshd's parsing isn't ideal, even if "understand the current configuration" is desired -- sshd -T should be consulted, instead of trying to mimic what sshd -T does.

Thanks

Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

I'm adding the openssh package to this bug, as the default configuration file has a Debian/Ubuntu-specific include directory configured and I think we should add an appropriate comment to inform the user that files included in the directory may override the configuration items in ssd_config. This wouldn't be something added by cloud-init, but something shipped in the openssh package itself.

Perhaps something like:

----
# The files included in the following directory may contain options
# that override the options in the current file. Configuration tools
# such as cloud-init may populate this directory in a default
# installation.
Include /etc/ssh/sshd_config.d/*.conf
----

Revision history for this message
Brett Holman (holmanb) wrote :

> the most technically advantageous and correct way to configure ssh

Agreed

> How is cloud-init making sure another file in sshd_config.d isn't superseding its config?

It isn't. I just filed an upstream bug to track this as an issue - and linked it above.

> sshd -T should be consulted, instead of trying to mimic what sshd -T does

Agreed, however ssh -G is more robust since -T is more likely to have failing tests.

> I'm adding the openssh package to this bug

Thanks! That approach sounds good to me.

Since the cloud-init (ubuntu) project exists to track issues in cloud-init on Ubuntu, I'm going to mark it as invalid for now. Feel free to change that back if you believe this to still be an issue with cloud-init.

Changed in cloud-init (Ubuntu):
status: New → Invalid
Nick Rosbrook (enr0n)
Changed in openssh (Ubuntu):
status: New → Triaged
importance: Undecided → Medium
assignee: nobody → Nick Rosbrook (enr0n)
tags: added: foundations-tidi
tags: added: foundations-todo
removed: foundations-tidi
Changed in cloud-init:
status: Unknown → New
Nick Rosbrook (enr0n)
Changed in openssh (Ubuntu):
status: Triaged → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package openssh - 1:9.9p1-3ubuntu2

---------------
openssh (1:9.9p1-3ubuntu2) plucky; urgency=medium

  * document /etc/ssh/sshd_config.d/*.conf better in sshd_config
    (LP: #2088207)
    - d/p/debian-config.patch: expand comment about configuration options
      and precedence of configuration snippets
    - d/openssh-server.ucf-md5sum: update for new sshd_config comments
    - d/p/sshd-socket-generator.patch: refresh for sshd_config comment
  * d/t/systemd-socket-activation: add wait while unit is reloading
    This avoids a race condition where we sometimes fail the assertion
    that ActiveState=active after systemctl reload.
    (LP: #2089049)

 -- Nick Rosbrook <email address hidden> Tue, 19 Nov 2024 12:06:14 -0500

Changed in openssh (Ubuntu):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public Security information  
Everyone can see this security related information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.