From nobody Thu Feb 24 22:35:53 2022 X-Original-To: bugs@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 5DBFB19DFEC1 for ; Thu, 24 Feb 2022 22:35:54 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4K4SRZ0d17z4wLV for ; Thu, 24 Feb 2022 22:35:54 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E5D90145DD for ; Thu, 24 Feb 2022 22:35:53 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 21OMZrFk023852 for ; Thu, 24 Feb 2022 22:35:53 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 21OMZrsF023851 for bugs@FreeBSD.org; Thu, 24 Feb 2022 22:35:53 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 262179] Prevent jail escaping via shared nullfs; option to disable UNIX domain socket binding Date: Thu, 24 Feb 2022 22:35:53 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 13.0-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: firk@cantconnect.ru X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter attachments.mimetype attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@freebsd.org MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645742154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vwfq7gt0McuFHWo7U+oqD4viKR6ylBzVkYszU9niAUY=; b=u8xgMs/Vm8vFk3xlGfymA1R9gfZTDG31yY0RiAbjGTkiFEIXcqNZv62nENzw5msagZFEIP YKHe1ckCKpiqJNo6CfycJkRcNqyDaFXwmz8r3SpOt0UfaqW0MtPpNN46q/u1dC29nyH7cI zcPA/9GRdo0+5+SLopzg5KGvXKrypNW1MhkZhfUeJPmZdTxFNg6IFUIbpN6TRT2Z6ai+VP QR/lzMgAcUBZuE5jzDwapmhq5+v+EmNdceQU9KEhvFsBW57cuqpObWSJEv71gt7nIh/5q7 tfsJeVxITiLHKCmeL9JPohE1JWUEEmHD/SlvYnp8zFOytn+KMo17kXOD9dmYfw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1645742154; a=rsa-sha256; cv=none; b=jdfmtVqiAxUy7laNuGq8Waht4bFz8ZsJmeeCusS6M3GzlxPbJTlyBL7DKPjb+t8+YRu4TK pi8r8Kf9vYDFHSmjkybDrOzBtblDfoj0wTCNfR+EbDAS5maWCw5e1GU/NSjIJ3/q56NoPI YIHLEBh293mMD/cNUFEUwIhqV81y0VEccqHskPFjps/bgrJlsvt9Aov5QVjPTiNrLNm4dd WPh1/UguADgGXD1kGxbcSqxhuQPILlUK1Wb86mNJq1NpFOnRL2Z6ZQoZqGX8WlvphBRrtW BpLcz5p2e/wPcqDH0wLElcO5bMauuRtr0L7Jr1hgbb7gq/V4veXEdwaxLm1b/Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D262179 Bug ID: 262179 Summary: Prevent jail escaping via shared nullfs; option to disable UNIX domain socket binding Product: Base System Version: 13.0-RELEASE Hardware: Any OS: Any Status: New Severity: Affects Some People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: firk@cantconnect.ru Attachment #232085 text/plain mime type: Created attachment 232085 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=3D232085&action= =3Dedit PoC source =3D=3D=3D=3D Background: possible security hole with jail & shared nullfs = =3D=3D=3D=3D When two independent jails have the same directory shared via nullfs, they = can escape chroot-ed filesystem. How this is done: 1) jail A creates listening UNIX domain socket in the shared directory. 2) jail B connects to that listening socket 3) jail A does open("/",O_DIRECTORY) and sends this fd over socket using SCM_RIGHTS 4) jail B receives the fd and now able to go through ".." into host filesys= tem Quick guide how to reproduce (sendfd.c in attachment): prepare: > gcc -o sendfd sendfd.c > mkdir /j /j/1 /j/2 > tar -c -f - /bin /lib /libexec | tar -x -f - -C /j/1 > tar -c -f - /bin /lib /libexec | tar -x -f - -C /j/2 > cp sendfd /j/1/bin > cp sendfd /j/2/bin > mkdir /j/1/shared /j/2/shared > mount -t nullfs /j/2/shared /j/1/shared first console: > jail /j/2 x 127.0.0.1 /bin/sendfd listen /shared/2.sock /bin second console: > jail /j/1 x 127.0.0.1 /bin/sendfd sh /shared/2.sock > pwd and you'll see pwd "/j/2" and may explore ../../ system root. =3D=3D=3D=3D Proposed fix =3D=3D=3D=3D New mount flag "nosockbind" means "do not allow UNIX domain bind()/bindat()= to paths on this filesystem". This flag is not transparent over nullfs, so it = is possible to mount bindable nullfs over non-bindable base partition. Note that connecting to UNIX domain socket on such filesystem is still possible. There is patches for 10.4, 11.4, 12.3 and 13-stable branches. At least 12.3 version looks working fine, but: VFS subsystem is quite complicated, and I'm new to it, so there are many (almst everywhere) things that I'm unsure: =3D=3D=3D vfs_mount.c =3D=3D=3D 1) I'm not sure if I should add the option just to global_opts[] in vfs_mou= nt.c or also to fs-specific lists (at least I've seen noexec and nosuid duplicat= ed from global_opts[] to ffs_opts[]) 2) I'm not sure about old sys_mount() API, there is some manual handling for ro/nosuid/noexec there. 3) I think user mounts (they automatically gets MNT_NOSUID) is not an issue here, am I right? =3D=3D=3D uipc_usrreq.c =3D=3D=3D 1) It seems that mp from vn_start_write is not always the direct mp for specified vnode (ex. for nullfs) so I read nd.ni_dvp->v_mount - is it corre= ct? It seems that ni_dvp can't be NULL here, and also it seems that it is locked and so can't disappear. 2) Could ni_dvp->v_mount be zero or spontaneously disappear in middle? I've added a check against NULL but may be it is not needed. =3D=3D=3D mount.h =3D=3D=3D Using unused flag 0x0000020000000000ULL for MNT_NOSOCKBIND. Somewhere in CURRENT MNT_RECURSE=3D0x0000100000000000ULL was added which is larger, but bits 0x00000E0000000000ULL still seems unused. =3D=3D=3D=3D What did not done =3D=3D=3D=3D Since nullfs is marked as jail-friendly, it seems that it is still possible= to do all this when jail created with allow_mount flag, by mounting unrestrict= ed nullfs over any place. Possible fixes for this: 1) enforce all jailed nullfs mounts to inherit "nosockbind" from underlying= fs 2) disallow jailed updating a mount from "nosockbind" to "nonosockbind" sta= te 3) make "nosockbind" transparent over nullfs (may be optional via sysctl) 4) workaround: do not make crossjail-shared nullfs accessible for allow_mou= nt jails --=20 You are receiving this mail because: You are the assignee for the bug.=