Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp951681rwd; Thu, 1 Jun 2023 08:33:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7rQxBgx8QfAspDBYAafMblhgA0Cbaxkb8f+ONt9sqlx4Tamhva0ODtCGmieZZIXgVXJ01p X-Received: by 2002:a17:902:ea0b:b0:1b0:7cc:982a with SMTP id s11-20020a170902ea0b00b001b007cc982amr2986382plg.5.1685633632222; Thu, 01 Jun 2023 08:33:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685633632; cv=none; d=google.com; s=arc-20160816; b=0FqxjQvS27z3+i3xIDno1gBqqPNKgYvOQPZNePukxliSytXIaXbRBZJvqfAxmT1iUl e+nvQRlSMOfGsu2d1F9surV/+pxOXzwut12ST5X2Nb2H+EoAy8pn4FKYoz678M0HnrKT L5E0RxEJUlvv3GWVDbJz0uhE15V9AX3rLXN+CvzNz0ogFHvueuyCWaccO8JXlbvRbSt6 uOpfpAyFgVmBb5CJf49ENKaBuGtxgcdtw0fsJPH+YsnPqee0Icf81+UP8IHPIdqdDlVM Hb7ljfcpWPmFTHrWpspFjMjI7ehw5Hdp+DHrRfhR1UaQf7aKaVTkfV4PEclepe+9dO3p qbeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-filter; bh=edxnEpFJ4Hj6NmttSexMjCk8JmvOYStgFd1bZIF7afQ=; b=SUfstTnoN8SI0aUuvwEt7hGqCtcpCrSU+jopzSGMoUVyhPy68aUW5Gp+UoGk2xiugh cEbixuJO4efJXB9b6m2D8NSFAx4b+DTURHyw0Q13WsX9pd/uKxSBw/qW3duZlGeSQarB AY1Myzrp0um2LsZZPwtErhuKLg0OhKImLVexryJOieU2349KGuqwj65tA3fb1CeHmvdV NPSj/1UWAn+8gu8rX6X0o0C8We0ILuzMIdSAc0WYNdMDuSQEqIcsYfIS4XsoQHYoBHMQ ocwTjAmtxu9gaVb8t5CeXO3nR3gSriWgINNe69kfxVCPMAq6XRUc2PX/vBBkIxyG6iEi nPhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=NSR5eNhc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d7-20020a170903230700b001ac671a8fd3si3030378plh.110.2023.06.01.08.33.35; Thu, 01 Jun 2023 08:33:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=NSR5eNhc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233747AbjFAPY0 (ORCPT + 99 others); Thu, 1 Jun 2023 11:24:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233650AbjFAPYY (ORCPT ); Thu, 1 Jun 2023 11:24:24 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 64C76180; Thu, 1 Jun 2023 08:24:21 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 615A520FC4F7; Thu, 1 Jun 2023 08:24:20 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 615A520FC4F7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1685633060; bh=edxnEpFJ4Hj6NmttSexMjCk8JmvOYStgFd1bZIF7afQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NSR5eNhcPEhH7MfW1yw1FJwRgl+eKjbWudIV1/WByc4CQc2BPzhqLnKopTO2Sp9tn 4mn53BZuLBcByf9kaHrn+aFF8peURqjbB+L/6Tcak83Q/G4STosugNVj8BryLtLZeS AR0kFWIKVCEFco0Lo5+oK/4Ak0KoYEQAdA8QdbuA= Date: Thu, 1 Jun 2023 08:24:14 -0700 From: Beau Belgrave To: Christian Brauner Cc: Alexei Starovoitov , Steven Rostedt , Masami Hiramatsu , LKML , linux-trace-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , bpf , David Vernet , Linus Torvalds , Dave Thaler , Christoph Hellwig Subject: Re: [PATCH] tracing/user_events: Run BPF program if attached Message-ID: <20230601152414.GA71@W11-BEAU-MD.localdomain> References: <20230509130111.62d587f1@rorschach.local.home> <20230509163050.127d5123@rorschach.local.home> <20230515165707.hv65ekwp2djkjj5i@MacBook-Pro-8.local> <20230515192407.GA85@W11-BEAU-MD.localdomain> <20230517003628.aqqlvmzffj7fzzoj@MacBook-Pro-8.local> <20230516212658.2f5cc2c6@gandalf.local.home> <20230517165028.GA71@W11-BEAU-MD.localdomain> <20230601-urenkel-holzofen-cd9403b9cadd@brauner> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230601-urenkel-holzofen-cd9403b9cadd@brauner> X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 01, 2023 at 11:46:13AM +0200, Christian Brauner wrote: > On Wed, May 17, 2023 at 05:10:47PM -0700, Alexei Starovoitov wrote: > > On Wed, May 17, 2023 at 9:50 AM Beau Belgrave wrote: > > > > > > > > > > > > > > Looks like user events were designed with intention to be unprivileged. > > > > > When I looked at kernel/trace/trace_events_user.c I assumed root. > > > > > I doubt other people reviewed it from security perspective. > > > > > > > > > > Recommending "chmod a+rw /sys/kernel/tracing/user_events_data" doesn't sound like a good idea. > > > > > > > > > > For example, I think the following is possible: > > > > > fd = open("/sys/kernel/tracing/user_events_data") > > > > > ioclt(fd, DIAG_IOCSDEL) > > > > > user_events_ioctl_del > > > > > delete_user_event(info->group, name); > > > > > > > > > > 'info' is different for every FD, but info->group is the same for all users/processes/fds, > > > > > because only one global init_group is created. > > > > > So one user can unregister other user event by knowing 'name'. > > > > > A security hole, no? > > > > ... > > > > > Regarding deleting events, only users that are given access can delete > > > events. They must know the event name, just like users with access to > > > delete files must know a path (and have access to it). Since the > > > write_index and other details are per-process, unless the user has > > > access to either /sys/kernel/tracing/events/user_events/* or > > > /sys/kernel/tracing/user_events_status, they do not know which names are > > > being used. > > > > > > If that is not enough, we could require CAP_SYSADMIN to be able to > > > delete events even when they have access to the file. Users can also > > > apply SELinux policies per-file to achieve further isolation, if > > > required. > > > > Whether /sys/kernel/tracing/user_events_status gets g+rw > > or it gets a+rw (as your documentation recommends) > > it is still a security issue. > > The "event name" is trivial to find out by looking at the source code > > of the target process or just "string target_binary". > > Restricting to cap_sysadmin is not the answer, since you want unpriv. > > SElinux is not the answer either. > > Since it's unpriv, different processes should not be able to mess with > > user events of other processes. > > It's a fundamental requirement of any kernel api. > > This has to be fixed before any bpf discussion. > > If it means that you need to redesign user_events do it now and > > excuses like "it's uapi now, so we cannot fix it" are not going to fly. > > Looking at this a little because I have a few minutes. > What's all this unused code? > These are stubs to integrate namespace support. I've been working on a series that adds a tracing namespace support similiar to the IMA namespace work [1]. That series is ending up taking more time than I anticipated. > static inline struct user_event_group > *user_event_group_from_user_ns(struct user_namespace *user_ns) > { > if (user_ns == &init_user_ns) > return init_group; > > return NULL; > } > > static struct user_event_group *current_user_event_group(void) > { > struct user_namespace *user_ns = current_user_ns(); > struct user_event_group *group = NULL; > > while (user_ns) { > group = user_event_group_from_user_ns(user_ns); > > if (group) > break; > > user_ns = user_ns->parent; > } > > return group; > } > > User namespaces form strict hierarchies so you always end up at > init_user_ns no matter where you start from in the hierarchy. Return the > init_group and delete that code above. > This is a good point, I'll delete this code and bring it back as part of the namespace support patch series when appropriate. > static char *user_event_group_system_name(struct user_namespace *user_ns) > { > char *system_name; > int len = sizeof(USER_EVENTS_SYSTEM) + 1; > > if (user_ns != &init_user_ns) { > /* > * Unexpected at this point: > * We only currently support init_user_ns. > * When we enable more, this will trigger a failure so log. > */ > pr_warn("user_events: Namespace other than init_user_ns!\n"); > return NULL; > } > > Your delegation model is premised on file permissions of a single file > in global tracefs. It won't work with user namespaces so let's not give > the false impression that this is on the table. > Users that are given access to the single file still should be able to be isolated for each other. The series I'm working on does this by changing the system name of user_events on a per-namespace basis. This prevents one namespace from messing with another, such as deleting their events, etc. I'll reach out to you for some time to discuss this direction to ensure I'm not off base. > Plus, all of this is also called in a single place during > trace_events_user_init() which is called from fs_initcall() so you > couldn't even pass a different user namespace if you wanted to because > only init_user_ns exists. In later series this is also called upon file open of user_events_data to find the right group, etc. I'll get this code removed for now and bring it back later. Thanks, -Beau 1. https://lore.kernel.org/linux-kernel/20230206140253.3755945-1-stefanb@linux.ibm.com/