Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754683AbbLZBKn (ORCPT ); Fri, 25 Dec 2015 20:10:43 -0500 Received: from thejh.net ([37.221.195.125]:59944 "EHLO thejh.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754296AbbLZBKl (ORCPT ); Fri, 25 Dec 2015 20:10:41 -0500 Date: Sat, 26 Dec 2015 02:10:38 +0100 From: Jann Horn To: Roland McGrath , Oleg Nesterov Cc: linux-kernel@vger.kernel.org, security@kernel.org, Serge Hallyn , Andy Lutomirski , "Eric W. Biederman" Subject: Re: [PATCH] ptrace: being capable wrt a process requires mapped uids/gids Message-ID: <20151226011038.GA25455@pc.thejh.net> References: <1449951161-4850-1-git-send-email-jann@thejh.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="azLHFNyN32YCQGCU" Content-Disposition: inline In-Reply-To: <1449951161-4850-1-git-send-email-jann@thejh.net> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3374 Lines: 82 --azLHFNyN32YCQGCU Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Dec 12, 2015 at 09:12:41PM +0100, Jann Horn wrote: > With this change, the entering process can first enter the > namespace and then safely inspect the namespace's > properties, e.g. through /proc/self/{uid_map,gid_map}, > assuming that the namespace owner doesn't have access to > uid 0. Actually, I think I missed something there. Well, at least it should not directly lead to a container escape. > -static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode) > +static bool ptrace_has_cap(const struct cred *tcred, unsigned int mode) > { > + struct user_namespace *tns = tcred->user_ns; > + struct user_namespace *curns = current_cred()->user_ns; > + > + /* When a root-owned process enters a user namespace created by a > + * malicious user, the user shouldn't be able to execute code under > + * uid 0 by attaching to the root-owned process via ptrace. > + * Therefore, similar to the capable_wrt_inode_uidgid() check, > + * verify that all the uids and gids of the target process are > + * mapped into the current namespace. > + * No fsuid/fsgid check because __ptrace_may_access doesn't do it > + * either. > + */ > + if (!kuid_has_mapping(curns, tcred->euid) || > + !kuid_has_mapping(curns, tcred->suid) || > + !kuid_has_mapping(curns, tcred->uid) || > + !kgid_has_mapping(curns, tcred->egid) || > + !kgid_has_mapping(curns, tcred->sgid) || > + !kgid_has_mapping(curns, tcred->gid)) > + return false; > + > if (mode & PTRACE_MODE_NOAUDIT) > - return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE); > + return has_ns_capability_noaudit(current, tns, CAP_SYS_PTRACE); > else > - return has_ns_capability(current, ns, CAP_SYS_PTRACE); > + return has_ns_capability(current, tns, CAP_SYS_PTRACE); > } If the namespace owner can run code in the init namespace, the kuids are mapped into curns but he is still capable wrt the target namespace. I think a proper fix should first determine the highest parent of tcred->user_ns in which the caller still has privs, then do the kxid_has_mapping() checks in there. --azLHFNyN32YCQGCU Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJWfekOAAoJED4KNFJOeCOoLXUP/3m7X5rOqRazMvKUujusPodG EWaPqKspyGIv/Xyh1iSimKK3jl4rqhNqEJeP0o1LQ08TmadiWt5iNhp7lXMYXI72 yKFXvBJX8LMSqqNipAEtdRfMt5HEDIpjWD4IKMaUSMHNGmWHIDL7LWjMP780AHsw lXftFuNIqOquxnEFhp/9w00UUbgA5iIIUua0HzRKuaJZIT5vOqOsK44p1xwCDlSt 2NARyBxmL+wAXYmMxS/7Ky3YEgfTHPYg4GY8MSegzlC0/bAxTrwURJJ1cA9TvrnM SrYdZZgikqCSz9MS6jZ+/caP/5p5uCtF1hOyhOdymOnZNt2HQDzPQsme+TXIgVGo IcNfOkHUHlaB5t7qBN6nUtlkQyaO5ldwVZ7qkJfHbL1RkkkawJXZEI4p3GyQWFKD iMOT/DyaPHTu1Kq/iR8We7zoG6BOqSeZscXQ33v1ezvCyxYaohknLvrj9K7H3Nnj EPK4HPwuW/qiSon4Znrz2sPaO08TgJwd3dQRc56HozpWbgrvLeGFCc4D9JNx8toP eTHlutltAGtUHu4+AHbeX5Y400Qg6oJ1wwiTEsTaopxuSjsk8yd4GGfrdJTCW6/6 2DFUz2dYJzEDYuGRzR3sNWwSt7glysbU6npS+Y0xSN8Ir134yAdo/TN5TxSlM+9/ JTlEiZoKMNHHPU0DJqWB =wrIv -----END PGP SIGNATURE----- --azLHFNyN32YCQGCU-- -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/