Bug 1498845 - ssh under newgrp'ed session fails hostbased authentication
Summary: ssh under newgrp'ed session fails hostbased authentication
Keywords:
Status: NEW
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: openssh
Version: 7.3
Hardware: x86_64
OS: Linux
medium
high
Target Milestone: rc
: ---
Assignee: Dmitry Belyavskiy
QA Contact: BaseOS QE Security Team
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2017-10-05 12:06 UTC by k-endou
Modified: 2022-11-03 10:57 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-11-09 07:31:26 UTC
Target Upstream Version:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Issue Tracker RHELPLAN-136107 0 None None None 2022-10-11 08:46:35 UTC
Red Hat Knowledge Base (Solution) 3384051 0 None None None 2018-03-16 12:44:07 UTC

Description k-endou 2017-10-05 12:06:13 UTC
Description of problem:
ssh with hostbased auth fails after changing group ID ( with newgrp, etc. )
although hostbased auth normally is accepted.
and ssh reports "setresgid XXXX: Operation not permitted".

Version-Release number of selected component (if applicable):
openssh-clients-7.4p1-11.el7.x86_64

How reproducible:
run ssh with both HostBasedAuthentication and EnableSSHKeysign enabled, after changing the group ID to one of the supplementary groups.

Steps to Reproduce:
1. run "newgrp" with one of the supplementary groups as its argument
2. under the newgrp'ed session, run ssh with hostbased auth enabled

Actual results:


Expected results:


Additional info:
comparing output;
-- normal --
  user1$ getent passwd user1
  user1:x:1001:1001::/home/user1:/bin/bash
  user1$ id
  uid=1001(user1) gid=1001(group1) groups=1001(group1),1002(group2)
  user1$ ssh -v -o HostbasedAuthentication=yes -o PubkeyAuthentication=no host1 hostname
  ...
  debug1: userauth_hostbased: trying hostkey ...
  debug1: permanently_drop_suid: 1001
  debug1: Authentication succeeded (hostbased).
  ...
  host1
  Transferred: ...
  Bytes per second: ...
  debug1: Exit status 0

-- chgrp'ed --
  user1$ getent passwd user1
  user1:x:1001:1001::/home/user1:/bin/bash
  user1$ newgrp group2
  user1$ id
  uid=1001(user1) gid=1002(group2) groups=1002(group2),1001(group1)
  user1$ ssh -v -o HostbasedAuthentication=yes -o PubkeyAuthentication=no host1 hostname
  ...
  debug1: userauth_hostbased: trying hostkey ...
  debug1: permanently_drop_suid: 1001
  setresgid 1001: Operation not permitted
  ssh_keysign: no reply
  sign using hostkey ... failed
  Permission denied (publickey,hostbased).

Comment 2 Jakub Jelen 2017-10-09 10:37:11 UTC
Did you make sure that the change was effective in your use case by re-logging in the user or using the - switch to newgrp?

With what are you comparing the "working" part? Was it working before? In which version?

Comment 3 k-endou 2017-10-10 01:33:59 UTC
(In reply to Jakub Jelen from comment #2)
> Did you make sure that the change was effective in your use case by
> re-logging in the user or using the - switch to newgrp?

"newgrp - group2" made no difference.

> With what are you comparing the "working" part? Was it working before? In
> which version?

I remember ssh in RHEL6 did not have problems like this, in other words,
I used to run ssh in newgrp'ed sessions and hostbased-auth did not fail.
But I can't verify that because I have no RHEL6 environment now.
Instead, I tried CentOS6/openssh-5.3p1-94.el6.x86_64
( hostname: mic, ssh client and server were on a same node )
and I found, as I expect, ssh did neither fail hostbased-auth nor report "setresgid XXXX: Operation not permitted" in a newgrp'ed session.

--- not newgrp'ed
user61$ getent passwd user61
user61:x:1101:1101::/home/user61:/bin/bash
user61$ id
uid=1101(user61) gid=1101(group61) groups=1101(group61),1102(group62)
user61$ ssh -v -o PubkeyAuthentication=no -o HostbasedAuthentication=yes localhost hostname
...
debug1: Next authentication method: hostbased
debug1: permanently_drop_suid: 1101
debug1: Remote: Accepted for localhost [::1] by /etc/ssh/shosts.equiv.
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: permanently_drop_suid: 1101
debug1: Remote: Accepted for localhost [::1] by /etc/ssh/shosts.equiv.
debug1: Authentication succeeded (hostbased).
...
mic
...
debug1: Exit status 0

-- newgrp'ed
user61$ newgrp group62
user61$ id
uid=1101(user61) gid=1102(group62) groups=1101(group61),1102(group62)
user61$ ssh -v -o PubkeyAuthentication=no -o HostbasedAuthentication=yes localhost hostname
...
debug1: Next authentication method: hostbased
debug1: permanently_drop_suid: 1101
debug1: Remote: Accepted for localhost [::1] by /etc/ssh/shosts.equiv.
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: permanently_drop_suid: 1101
debug1: Remote: Accepted for localhost [::1] by /etc/ssh/shosts.equiv.
debug1: Authentication succeeded (hostbased).
...
mic
...
debug1: Exit status 0

Comment 5 Jakub Jelen 2017-10-10 14:16:59 UTC
ok, forget about the previous comment. It is going through different code paths. Here is a debug3 log as I reproduced it with the latest openssh:

debug3: authmethod_is_enabled hostbased
debug1: Next authentication method: hostbased
debug3: userauth_hostbased: trying key type ecdsa-sha2-nistp256-cert-v01
debug3: userauth_hostbased: trying key type ecdsa-sha2-nistp384-cert-v01
debug3: userauth_hostbased: trying key type ecdsa-sha2-nistp521-cert-v01
debug3: userauth_hostbased: trying key type ssh-ed25519-cert-v01
debug3: userauth_hostbased: trying key type ssh-rsa-cert-v01
debug3: userauth_hostbased: trying key type ecdsa-sha2-nistp256
debug1: userauth_hostbased: trying hostkey ecdsa-sha2-nistp256 SHA256:OyN0FVZ1BiQ5cgEY8L7+jrEMuWRXarBBGs7Faw1mDgA
debug2: userauth_hostbased: chost f26.
debug3: ssh_msg_send: type 2
debug3: ssh_msg_recv entering
debug1: permanently_drop_suid: 1001
debug3: ssh_keysign: [child] pid=14766, exec /usr/libexec/openssh/ssh-keysign
setresgid 1002: Operation not permitted
ssh_keysign: no reply
sign using hostkey ecdsa-sha2-nistp256 SHA256:OyN0FVZ1BiQ5cgEY8L7+jrEMuWRXarBBGs7Faw1mDgA failed
debug2: we did not send a packet, disable method

The error is not coming from the ssh process, but from the exec-ed ssh-keysign, which does one of the first things permanently_set_uid(pw). This call was there quite much ever since (2004) so I don't think it could be something that changed in OpenSSH.

The problem is that ssh-keysign is a setgid binary, so it is already running as "ssh_keys" group, but RHEL6 and upstream is the root account and the binary is setuid. This was changed in RHEL7 to harden SSHD daemon. I can not find Fedora/RHEL bugs, but there is an upstream bug which was closed as wontfix even though it is in Fedora:

https://bugzilla.mindrot.org/show_bug.cgi?id=1893

What I remember, this was the first and only issue with this change over the years. Workaround is probably to revert from setgit to setuid and some related downstream changes, but I am not sure if we want to go down this road at this moment.

If this issue is critical or in any way time sensitive, please raise a ticket through the regular Red Hat support channels to ensure it receives the proper attention and prioritization to assure a timely resolution. 

For information on how to contact the Red Hat production support team, please visit:
    https://www.redhat.com/support/process/production/#howto

Comment 6 k-endou 2017-10-12 01:11:32 UTC
Thank you for your suggestion.

But now I understand what caused this problem and how to get around it, so it's no longer emergency for me.
I've decided to add setuid permission to the ssh-keysign binary, and that actually is preventing the problem.

I don't know this method - to add setuid permission - should be best or not though, I'd like RHEL/Fedora or upstream to apply this or some other correction, or at least, to add some explanation to man or some document.
because it's a bit confusing behavior that newgrp'ed ssh fails hostbased-auth while ssh which is not newgrp'ed succeeds.

Comment 7 Jakub Jelen 2017-10-12 12:36:19 UTC
Thank you for the confirmation that setuid bit helped. The setuid bit was there until RHEL6, so it should be quite safe to restore it in your case.

I am not sure if we will be able to change this in RHEL7 timeframe (unless it shows as a bigger issue -- this is probably the first problem we found with this approach since 2011 [1]).

This change was no significant security improvement and was never accepted in upstream openssh so I believe we could switch back in the next major release to avoid differences from upstream OpenSSH.

Until that time, the easiest and probably best solution where to document this issue would be a Knowledge base on customer portal, which is written by the support guys and used by them and customers. They are usually created based on the customer issues so filling a ticket on customer portal would be the fastest way there.

[1] https://src.fedoraproject.org/rpms/openssh/c/1ddd0ee5


Note You need to log in before you can comment on or make changes to this bug.