Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10673498imu; Mon, 31 Dec 2018 04:34:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN4AGr6nW/bT3JuPkiiTbfD4RlbL/rTc4UJvY7q8iNpTB/j7gyvqWYabfdVWvrDe/NpBW2zl X-Received: by 2002:a17:902:3281:: with SMTP id z1mr37676853plb.296.1546259678519; Mon, 31 Dec 2018 04:34:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546259678; cv=none; d=google.com; s=arc-20160816; b=J0XwWiuFk5JpiZCXore6kYUnOyEfQb06mn10ZL8a3r89x0H6wv5hU8wbd9NULrsJFh ugqW1YIj3mDpS0eOlLQbtCCQ6NFAS4jQiwWauogsoMwo0N2/VgeLaHRw0ruL76GbHdVD 1QVYrms8SZNrxIFhkdJSJf1Hclty12cmWVN2IYEFfQ+koVzSTgdDlV+S2skfpQoJ6+9/ 4dTRqjBlUWT4lmsFDNCmr/G8nUGUUYJNU+EcrlEMEma5qhmu1tZtsKOMsvCT4bM3PyVr yeD2MMpUvHe3MHXANMJcsJ8l4iMuVMjgtDSQpMIrVX/Tkl1s+PttWrszAFx8NlV6e3Mk Ipjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=wwY+UDf3YX4wUlNXwlxxRRKQM2nm4enr6oFMLu52vi8=; b=Xne1/LD+t8eUaA0KbjNJD5eX8gWzUvbFCNaDADBJEwx+HXfpoRCzBda0JvCYf7ItN+ IKL0kVU62NPbyo2/OJzVjgkpjtVcgVILuIO8zIEcwe0VS3CXb1LGNAqoJKhTp/oBlZZD KYKP8GGWk+jcbFJiKaDsA4H7q22ifDk2GfUaL+0w2rpvGHBUA5n6/W65cU2R7V8VGrBT geXlyNypX5A9lEzHnjeRCNMh7MzfAbeyuqTre/aPVppLQ7yqQSz2T6YsTjfQ3lUzEW+b FFraKnoe+R845R34/0Z7Ce4WxSnumFD+HnhUmKJpFFnIdG0fGUzJAF/nRvv2FC7mVfiX DIoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=m+bBSVjn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o12si42326940plg.250.2018.12.31.04.34.22; Mon, 31 Dec 2018 04:34:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=m+bBSVjn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727369AbeLaMdO (ORCPT + 99 others); Mon, 31 Dec 2018 07:33:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:50646 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727269AbeLaMdO (ORCPT ); Mon, 31 Dec 2018 07:33:14 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A8F7420828; Mon, 31 Dec 2018 12:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1546259593; bh=bPiGrds328Rm3IVL0tgzfV0uDvTk/4rwY70aDA90ZmM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=m+bBSVjno0i9gC8lD0yIejGqpz6dBQqwD+6LN53VxpjzrNHs1w6WLkXGm3VBuLbCy tyV9dgBTuolya6sPMwHswLUsP/jzqneDaEKjUSuIkI893QX0nzqhaeJhpe7x0PCWn2 RccaWN66eEIxGzDuiRwxjtvvLoQ81AMjijGvY1TU= Date: Mon, 31 Dec 2018 13:33:10 +0100 From: Greg Kroah-Hartman To: Jann Horn Cc: joeyli , Andy Lutomirski , "Lee, Chun-Yi" , "Rafael J . Wysocki" , Pavel Machek , Len Brown , "Martin K . Petersen" , Randy Dunlap , Joe Perches , Bart Van Assche , kernel list , linux-pm@vger.kernel.org, Chen Yu , Giovanni Gherdovich Subject: Re: [PATCH 2/2] PM / Sleep: Check the file capability when writing wake lock interface Message-ID: <20181231123310.GA3038@kroah.com> References: <20181230132856.24095-1-jlee@suse.com> <20181230132856.24095-3-jlee@suse.com> <20181230144835.GB18985@kroah.com> <20181231093851.GN3506@linux-l9pv.suse> <20181231104055.GB27420@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.11.1 (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 31, 2018 at 01:02:35PM +0100, Jann Horn wrote: > On Mon, Dec 31, 2018 at 11:41 AM Greg Kroah-Hartman > wrote: > > > > On Mon, Dec 31, 2018 at 05:38:51PM +0800, joeyli wrote: > > > Hi Greg, > > > > > > On Sun, Dec 30, 2018 at 03:48:35PM +0100, Greg Kroah-Hartman wrote: > > > > On Sun, Dec 30, 2018 at 09:28:56PM +0800, Lee, Chun-Yi wrote: > > > > > The wake lock/unlock sysfs interfaces check that the writer must has > > > > > CAP_BLOCK_SUSPEND capability. But the checking logic can be bypassed > > > > > by opening sysfs file within an unprivileged process and then writing > > > > > the file within a privileged process. The tricking way has been exposed > > > > > by Andy Lutomirski in CVE-2013-1959. > > > > > > > > Don't you mean "open by privileged and then written by unprivileged?" > > > > Or if not, exactly how is this a problem? You check the capabilities > > > > when you do the write and if that is not allowed then, well > > > > > > > > > > Sorry for I didn't provide clear explanation. > > > > > > The privileged means CAP_BLOCK_SUSPEND but not file permission. The file permission > > > has already relaxed for non-root user. Then the expected behavior is that non-root > > > process must has CAP_BLOCK_SUSPEND capability for writing wake_lock sysfs. > > > > > > But, the CAP_BLOCK_SUSPEND restrict can be bypassed: > > > > > > int main(int argc, char* argv[]) > > > { > > > int fd, ret = 0; > > > > > > fd = open("/sys/power/wake_lock", O_RDWR); > > > if (fd < 0) > > > err(1, "open wake_lock"); > > > > > > if (dup2(fd, 1) != 1) // overwrite the stdout with wake_lock > > > err(1, "dup2"); > > > sleep(1); > > > execl("./string", "string"); //string has capability > > > > > > return ret; > > > } > > > > > > This program is an unpriviledged process (has no CAP_BLOCK_SUSPEND), it opened > > > wake_lock sysfs and overwrited stdout. Then it executes the "string" program > > > that has CAP_BLOCK_SUSPEND. > > > > That's the problem right there, do not give CAP_BLOCK_SUSPEND rights to > > "string". If any user can run that program, there's nothing the kernel > > can do about this, right? Just don't allow that program on the system :) > > > > > The string program writes to stdout, which means that it writes to > > > wake_lock. So an unpriviledged opener can trick an priviledged writer > > > for writing sysfs. > > > > That sounds like a userspace program that was somehow given incorrect > > rights by the admin, and a user is taking advantage of it. That's not > > the kernel's fault. > > Isn't it? Pretty much any setuid program will write to stdout or > stderr; even the glibc linker code does so if you set LD_DEBUG. > (Normally the output isn't entirely attacker-controlled, but it is in > the case of stuff like "procmail", which I think Debian still ships as > setuid root.) setuid programs should always be able to safely call > read() and write() on caller-provided file descriptors. Also, you're > supposed to be able to receive file descriptors over unix domain > sockets and then write to them without trusting the sender. Basically, > the ->read and ->write VFS handlers should never look at the caller's > credentials, only the opener's (with the exception of LSMs, which tend > to do weird things to the system's security model). So a root program gets the file handle to the sysfs file and then passes it off to a setuid program and the kernel should somehow protect from this? I think that any sysfs file that is relying on the capable() check should just set their permissions properly first, and then it should be ok. thanks, greg k-h