Received: by 10.223.185.116 with SMTP id b49csp4537601wrg; Mon, 26 Feb 2018 20:57:13 -0800 (PST) X-Google-Smtp-Source: AH8x225MqqSTH1MtXhLWdeyq5jdFU8OZEgXxzJ9IsBWy0XszdcRlfj5bZ8b6bouNowy1e3kUrN7A X-Received: by 2002:a17:902:8488:: with SMTP id c8-v6mr13039110plo.155.1519707433555; Mon, 26 Feb 2018 20:57:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519707433; cv=none; d=google.com; s=arc-20160816; b=NWgVHUXBaoolRg3oMJaghqR6C/U9Mzz8KhmGSz1U/uYxIlQZqW+6ByPQNVaHeSYQ5+ CizU1i1r8QjhNeFokhrsnNkKAQaZArQjGRXssYnB94SRPJmpjaukQlGtXZj1BkVaYxPL E2qutA/4Mephp2RC+xQUixTMYbRjmV8SyrgY6aOUeDlZUXwyVDMci+Lj4IC4lNO6Mrsv ZCGMLOM0TjTWEByG1/kkb0bWNeld9V4iR8bEwto6EiRMmbcLTHyhOUfFSLAi/0J8LDmi olgHpr5Iadeil+SzCFS2D3rHpaD00C0hnZH8/6AM1RWjPYv6X5XoIl8NOyi8Cnl+0SKf nGfA== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=NxkNd/ww53MsqO/Vm/65XRRbiip8rHTFh5ASlyypT0E=; b=CRWvYWIlssET6tbefMEnoxe0XchGJFRsIM9i56J2iK1oe0faXJhq/VMymJDGgsBozc PNrkRRwBW/pIZ8sxr4gEm4bQQPx7UUxoezFXm+9jWkOSsT9Cb8lT0rSHiIh5R/5WeCvg yAHvzx3MjMuB/LgLghZqzdt2mesospfYtD3hKgdnm6yeFDYljHua4tG5JFYTaDktLpBp NsQ8Q8TyGegPqkhPISHiVh0pY7PuPyABLrZUL9WfQjrA+zJ+ATZCQfVW+Cn+EeycA0No gQKu5n/xt9OnvcdnBWoGl7XeKSPdReNDbJp9ezVOl0fpTS2Qg76VFh7J68+jVWf2To3A x0UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=U3VZRzO2; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q11si7894666pfd.242.2018.02.26.20.56.59; Mon, 26 Feb 2018 20:57:13 -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=@gmail.com header.s=20161025 header.b=U3VZRzO2; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752004AbeB0EzH (ORCPT + 99 others); Mon, 26 Feb 2018 23:55:07 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:32770 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751664AbeB0EzE (ORCPT ); Mon, 26 Feb 2018 23:55:04 -0500 Received: by mail-pl0-f67.google.com with SMTP id c11-v6so7310250plo.0; Mon, 26 Feb 2018 20:55:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=NxkNd/ww53MsqO/Vm/65XRRbiip8rHTFh5ASlyypT0E=; b=U3VZRzO23nASIhXCMt+mdl3HGpHMChnwkQrHhwu9kM/MriJ5uuVvQGEN1EDmCSAyYr bF7D8FheCOEpDhEjr+hNHcrJiQyFEgF0hHR8IAr6zbRyMjcWYJLJYaot92TchOTp3QM+ PNtd3P85x5E4bj23rKXhJCxfmijaU9anNNCVpzfpVtGHPDjwUfQLvpsFb8AAM6ESPAiC f7zLD0L9jPCyvmb4+ZjOTp00N357XCVHZkTuH+1pRtMPzkKYVtvjVluKaK4bUBFFKrxn hbqruSTlux6LJPzQGuf+EBx7iE+0qOab7VPDYQ9ZfFh6f3isUnjAmOlwTZus77f6ct4/ YSfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=NxkNd/ww53MsqO/Vm/65XRRbiip8rHTFh5ASlyypT0E=; b=lhOpUbvj7sTYf4pu+X75wsbu9u9kZZnoXuZfMZaJWggWW248+VbQHcHTckX/8woe+b Yfwa1JLH9a2otLDopOTnHaxCXoNM9gUsmaLqcEzZRe07RQGQ+IJiJNpP+ACpOwpMV1lF 1Izso9WMd9n7BNF5JT2uAf/Uwge39jg99bWfadxSoY4D4oFepU3RDGCKbnr9zDKYyyi1 SHxQjy+RzoXb9qY1wa6pGOQt9hzyBPLx+W1yuN1XPSVrEnLGiiBJZmJtr1YHo0N5LaGQ 1zXI/N/r1w3WX3mfiXA+Mf+DxV5ABpElR/lnXAkdI1Z71e8ov7Blm/SGQ8FeNC4lXQhy 9wRw== X-Gm-Message-State: APf1xPA0e6uOnJ91ydqwjt3HSXh0VLTJmJwCJAjqG7jFOOOaZMnlGoPU UWhs9sr0sW/rqoLB9i+BtJg= X-Received: by 2002:a17:902:8646:: with SMTP id y6-v6mr13267074plt.406.1519707303705; Mon, 26 Feb 2018 20:55:03 -0800 (PST) Received: from ast-mbp ([2620:10d:c090:180::1:c617]) by smtp.gmail.com with ESMTPSA id 83sm20927052pfj.151.2018.02.26.20.55.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Feb 2018 20:55:02 -0800 (PST) Date: Mon, 26 Feb 2018 20:54:59 -0800 From: Alexei Starovoitov To: Andy Lutomirski Cc: =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= , 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 Subject: Re: [PATCH bpf-next v8 05/11] seccomp,landlock: Enforce Landlock programs per process hierarchy Message-ID: <20180227045458.wjrbbsxf3po656du@ast-mbp> References: <20180227004121.3633-1-mic@digikod.net> <20180227004121.3633-6-mic@digikod.net> <20180227020856.teq4hobw3zwussu2@ast-mbp> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20170421 (1.8.2) 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 04:40:34AM +0000, Andy Lutomirski wrote: > 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. it doesn't look to me that existing seccomp side of managing fork situation can be reused. Here there is an attempt to add 'chaining' concept which sort of an extension of existing seccomp style, but somehow heavily done on bpf side and contradicts cgroup/tracing.