Received: by 10.223.185.116 with SMTP id b49csp4527782wrg; Mon, 26 Feb 2018 20:41:50 -0800 (PST) X-Google-Smtp-Source: AH8x225k5/GD6wnJA66ZguFtp53xb1wzAWTJP22uyqqbQJ4NxSkbigmRjcci0JQ2wuDSD5JCWvND X-Received: by 10.99.103.69 with SMTP id b66mr10245843pgc.233.1519706510773; Mon, 26 Feb 2018 20:41:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519706510; cv=none; d=google.com; s=arc-20160816; b=Gv7XIYb+OypL9Wz+dVg1iByvR9kuhVOPJf2z8F0Y6/vgRVxow7DjY7GBhdfrOr8m9F 3O03kz5V6aHnO/EFCdp2WM12gMQUHncjCnXsL/t27pOBgzUE5eCQsyrVFwJJbyQAbqDS bR0SnKaZWMZB58aK3FR4vWRZ3jD/eB2Ed7jl7f6I/TsOSAhW+fssiw9b36+4kOjMjb3m SlUJzpGuA8MiTekelmSpqTCOU4PGqmxIRHaEzxuDPhG+u0/3c+MJKqWNTkuC9N+02SDK B88iQkiIoC0N3X5JQhKthBU3yZW6SAxzGNZ1OnJvLaZVB/WPphEYg6cHDnLSx27ny3xm CT0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dmarc-filter:arc-authentication-results; bh=qVI5e13IuL8HLQU/gsKGmZ3qPopbtaMtP2e2+HnTI3o=; b=RnZSouFP3C8xxfrvRqitozmzKofQB+e6KPR/h+ECJgjsrrZaAJfoUT7XhZ0WwfVx1N udLBsuI1YhyCN3og/qQGI5Po8GorpB+AIBDV/TBThKfZ2QdaGHX0JjLUrjKHLKtICYgv S8rhqE2ezSN5KuMg2FrqGMjnenfCNB2E11g59mMbD8mZ9lKOVB0L3yrWF7ad2/J8pmgp 1Lvj+gnwk1/dm/QbTp4eifAWahyYWN3/TVZgArlxexhaT0LjkFfhh7I/J97hyZUFUECq YHJl6TmLu8jP8tsj13oL3+b6bktMBoZnIwB/nM+hkKO+Sxv1mcr0c+mLI48CBEiwPbpG o3LA== ARC-Authentication-Results: i=1; mx.google.com; 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 l71si6460628pgd.501.2018.02.26.20.41.35; Mon, 26 Feb 2018 20:41:50 -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; 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 S1751721AbeB0Ek5 convert rfc822-to-8bit (ORCPT + 99 others); Mon, 26 Feb 2018 23:40:57 -0500 Received: from mail.kernel.org ([198.145.29.99]:41760 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751501AbeB0Ek4 (ORCPT ); Mon, 26 Feb 2018 23:40:56 -0500 Received: from mail-it0-f44.google.com (mail-it0-f44.google.com [209.85.214.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6060C21777 for ; Tue, 27 Feb 2018 04:40:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6060C21777 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=luto@kernel.org Received: by mail-it0-f44.google.com with SMTP id n128so210289ith.1 for ; Mon, 26 Feb 2018 20:40:55 -0800 (PST) X-Gm-Message-State: APf1xPA9BEOqKCByNz3xX12SUVu+87dEHlEMEGsKshpT0pbTEEiXUNT+ pSVOdN2fl/mIzuApcduAYA1FO4MxoZ6uucgHHjB/pw== X-Received: by 10.36.222.134 with SMTP id d128mr16206967itg.150.1519706454722; Mon, 26 Feb 2018 20:40:54 -0800 (PST) MIME-Version: 1.0 Received: by 10.2.137.101 with HTTP; Mon, 26 Feb 2018 20:40:34 -0800 (PST) In-Reply-To: <20180227020856.teq4hobw3zwussu2@ast-mbp> References: <20180227004121.3633-1-mic@digikod.net> <20180227004121.3633-6-mic@digikod.net> <20180227020856.teq4hobw3zwussu2@ast-mbp> From: Andy Lutomirski Date: Tue, 27 Feb 2018 04:40:34 +0000 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf-next v8 05/11] seccomp,landlock: Enforce Landlock programs per process hierarchy To: Alexei Starovoitov Cc: =?UTF-8?B?TWlja2HDq2wgU2FsYcO8bg==?= , LKML , Alexei Starovoitov , Arnaldo Carvalho de Melo , Casey Schaufler , Daniel Borkmann , David Drysdale , "David S . Miller" , "Eric W . Biederman" , James Morris , Jann Horn , Jonathan Corbet , Michael Kerrisk , Kees Cook , Paul Moore , Sargun Dhillon , "Serge E . Hallyn" , Shuah Khan , Tejun Heo , Thomas Graf , Tycho Andersen , Will Drewry , Kernel Hardening , Linux API , LSM List , Network Development , Andrew Morton Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 27, 2018 at 2:08 AM, Alexei Starovoitov wrote: > On Tue, Feb 27, 2018 at 01:41:15AM +0100, Mickaël Salaün wrote: >> The seccomp(2) syscall can be used by a task to apply a Landlock program >> to itself. As a seccomp filter, a Landlock program is enforced for the >> current task and all its future children. A program is immutable and a >> task can only add new restricting programs to itself, forming a list of >> programss. >> >> A Landlock program is tied to a Landlock hook. If the action on a kernel >> object is allowed by the other Linux security mechanisms (e.g. DAC, >> capabilities, other LSM), then a Landlock hook related to this kind of >> object is triggered. The list of programs for this hook is then >> evaluated. Each program return a 32-bit value which can deny the action >> on a kernel object with a non-zero value. If every programs of the list >> return zero, then the action on the object is allowed. >> >> Multiple Landlock programs can be chained to share a 64-bits value for a >> call chain (e.g. evaluating multiple elements of a file path). This >> chaining is restricted when a process construct this chain by loading a >> program, but additional checks are performed when it requests to apply >> this chain of programs to itself. The restrictions ensure that it is >> not possible to call multiple programs in a way that would imply to >> handle multiple shared values (i.e. cookies) for one chain. For now, >> only a fs_pick program can be chained to the same type of program, >> because it may make sense if they have different triggers (cf. next >> commits). This restrictions still allows to reuse Landlock programs in >> a safe way (e.g. use the same loaded fs_walk program with multiple >> chains of fs_pick programs). >> >> Signed-off-by: Mickaël Salaün > > ... > >> +struct landlock_prog_set *landlock_prepend_prog( >> + struct landlock_prog_set *current_prog_set, >> + struct bpf_prog *prog) >> +{ >> + struct landlock_prog_set *new_prog_set = current_prog_set; >> + unsigned long pages; >> + int err; >> + size_t i; >> + struct landlock_prog_set tmp_prog_set = {}; >> + >> + if (prog->type != BPF_PROG_TYPE_LANDLOCK_HOOK) >> + return ERR_PTR(-EINVAL); >> + >> + /* validate memory size allocation */ >> + pages = prog->pages; >> + if (current_prog_set) { >> + size_t i; >> + >> + for (i = 0; i < ARRAY_SIZE(current_prog_set->programs); i++) { >> + struct landlock_prog_list *walker_p; >> + >> + for (walker_p = current_prog_set->programs[i]; >> + walker_p; walker_p = walker_p->prev) >> + pages += walker_p->prog->pages; >> + } >> + /* count a struct landlock_prog_set if we need to allocate one */ >> + if (refcount_read(¤t_prog_set->usage) != 1) >> + pages += round_up(sizeof(*current_prog_set), PAGE_SIZE) >> + / PAGE_SIZE; >> + } >> + if (pages > LANDLOCK_PROGRAMS_MAX_PAGES) >> + return ERR_PTR(-E2BIG); >> + >> + /* ensure early that we can allocate enough memory for the new >> + * prog_lists */ >> + err = store_landlock_prog(&tmp_prog_set, current_prog_set, prog); >> + if (err) >> + return ERR_PTR(err); >> + >> + /* >> + * Each task_struct points to an array of prog list pointers. These >> + * tables are duplicated when additions are made (which means each >> + * table needs to be refcounted for the processes using it). When a new >> + * table is created, all the refcounters on the prog_list are bumped (to >> + * track each table that references the prog). When a new prog is >> + * added, it's just prepended to the list for the new table to point >> + * at. >> + * >> + * Manage all the possible errors before this step to not uselessly >> + * duplicate current_prog_set and avoid a rollback. >> + */ >> + if (!new_prog_set) { >> + /* >> + * If there is no Landlock program set used by the current task, >> + * then create a new one. >> + */ >> + new_prog_set = new_landlock_prog_set(); >> + if (IS_ERR(new_prog_set)) >> + goto put_tmp_lists; >> + } else if (refcount_read(¤t_prog_set->usage) > 1) { >> + /* >> + * If the current task is not the sole user of its Landlock >> + * program set, then duplicate them. >> + */ >> + new_prog_set = new_landlock_prog_set(); >> + if (IS_ERR(new_prog_set)) >> + goto put_tmp_lists; >> + for (i = 0; i < ARRAY_SIZE(new_prog_set->programs); i++) { >> + new_prog_set->programs[i] = >> + READ_ONCE(current_prog_set->programs[i]); >> + if (new_prog_set->programs[i]) >> + refcount_inc(&new_prog_set->programs[i]->usage); >> + } >> + >> + /* >> + * Landlock program set from the current task will not be freed >> + * here because the usage is strictly greater than 1. It is >> + * only prevented to be freed by another task thanks to the >> + * caller of landlock_prepend_prog() which should be locked if >> + * needed. >> + */ >> + landlock_put_prog_set(current_prog_set); >> + } >> + >> + /* prepend tmp_prog_set to new_prog_set */ >> + for (i = 0; i < ARRAY_SIZE(tmp_prog_set.programs); i++) { >> + /* get the last new list */ >> + struct landlock_prog_list *last_list = >> + tmp_prog_set.programs[i]; >> + >> + if (last_list) { >> + while (last_list->prev) >> + last_list = last_list->prev; >> + /* no need to increment usage (pointer replacement) */ >> + last_list->prev = new_prog_set->programs[i]; >> + new_prog_set->programs[i] = tmp_prog_set.programs[i]; >> + } >> + } >> + new_prog_set->chain_last = tmp_prog_set.chain_last; >> + return new_prog_set; >> + >> +put_tmp_lists: >> + for (i = 0; i < ARRAY_SIZE(tmp_prog_set.programs); i++) >> + put_landlock_prog_list(tmp_prog_set.programs[i]); >> + return new_prog_set; >> +} > > Nack on the chaining concept. > Please do not reinvent the wheel. > There is an existing mechanism for attaching/detaching/quering multiple > programs attached to cgroup and tracing hooks that are also > efficiently executed via BPF_PROG_RUN_ARRAY. > Please use that instead. > I don't see how that would help. Suppose you add a filter, then fork(), and then the child adds another filter. Do you want to duplicate the entire array? You certainly can't *modify* the array because you'll affect processes that shouldn't be affected. In contrast, doing this through seccomp like the earlier patches seemed just fine to me, and seccomp already had the right logic.