From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D9F8C433E0 for ; Tue, 22 Dec 2020 17:35:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C448622583 for ; Tue, 22 Dec 2020 17:35:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727696AbgLVRf0 (ORCPT ); Tue, 22 Dec 2020 12:35:26 -0500 Received: from out01.mta.xmission.com ([166.70.13.231]:50134 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727084AbgLVRf0 (ORCPT ); Tue, 22 Dec 2020 12:35:26 -0500 Received: from in01.mta.xmission.com ([166.70.13.51]) by out01.mta.xmission.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1krlYY-00EzCF-Go; Tue, 22 Dec 2020 10:34:42 -0700 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.87) (envelope-from ) id 1krlYW-0000Fv-VN; Tue, 22 Dec 2020 10:34:42 -0700 From: ebiederm@xmission.com (Eric W. Biederman) To: Tetsuo Handa Cc: Al Viro , Jens Axboe , Christoph Hellwig , Kees Cook , LKML References: <0bcc0c63-31a3-26fd-bccb-b28af0375c34@i-love.sakura.ne.jp> Date: Tue, 22 Dec 2020 11:33:58 -0600 In-Reply-To: <0bcc0c63-31a3-26fd-bccb-b28af0375c34@i-love.sakura.ne.jp> (Tetsuo Handa's message of "Tue, 22 Dec 2020 23:39:08 +0900") Message-ID: <87a6u5iw3d.fsf@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1krlYW-0000Fv-VN;;;mid=<87a6u5iw3d.fsf@x220.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/YCQJ6h27iCJXzD92zJcYHBr4oU/lpD/g= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: Does uaccess_kernel() work for detecting kernel thread? X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Tetsuo Handa writes: > Commit db68ce10c4f0a27c ("new helper: uaccess_kernel()") replaced segment_eq(get_fs(), KERNEL_DS) > with uaccess_kernel(). But uaccess_kernel() became an unconditional "false" for some architectures > due to commit 5e6e9852d6f76e01 ("uaccess: add infrastructure for kernel builds with set_fs()") and > follow up changes in Linux 5.10. As a result, I guess that uaccess_kernel() can no longer be used > as a condition for checking whether current thread is a kernel thread or not. > > For example, if uaccess_kernel() is "false" due to CONFIG_SET_FS=n, > isn't sg_check_file_access() failing to detect kernel context? > > static int sg_check_file_access(struct file *filp, const char *caller) > { > if (filp->f_cred != current_real_cred()) { > pr_err_once("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n", > caller, task_tgid_vnr(current), current->comm); > return -EPERM; > } > if (uaccess_kernel()) { > pr_err_once("%s: process %d (%s) called from kernel context, this is not allowed.\n", > caller, task_tgid_vnr(current), current->comm); > return -EACCES; > } > return 0; > } > > For another example, if uaccess_kernel() is "false" due to CONFIG_SET_FS=n, > isn't TOMOYO unexpectedly checking permissions for socket operations? > > static bool tomoyo_kernel_service(void) > { > /* Nothing to do if I am a kernel service. */ > return uaccess_kernel(); > } > > static u8 tomoyo_sock_family(struct sock *sk) > { > u8 family; > > if (tomoyo_kernel_service()) > return 0; > family = sk->sk_family; > switch (family) { > case PF_INET: > case PF_INET6: > case PF_UNIX: > return family; > default: > return 0; > } > } > > Don't we need to replace such usage with something like (current->flags & PF_KTHREAD) ? > I don't know about io_uring, but according to > https://lkml.kernel.org/r/dacfb329-de66-d0cf-dcf9-f030ea1370de@schaufler-ca.com , > should (current->flags & (PF_KTHREAD | PF_IO_WORKER)) == PF_KTHREAD be used instead? I think you are reading the situation properly. I skimmed the tomoyo code and it appears that you are excluding kernel threads so as not to limit kernel threads such as nfsd. For PF_IO_WORKER kernel threads which are running code on behalf of a user we want to perform the ordinary permission checks. So you want the idiom you pasted above. I do wonder though if perhaps we should create a is_user_cred helper to detect the difference between the creds of kernel threads and the thread of ordinary userspace. Which would handle io_uring that copy creds around and check them at a later time more cleanly. Eric