Received: by 10.223.164.221 with SMTP id h29csp757241wrb; Wed, 11 Oct 2017 08:41:34 -0700 (PDT) X-Received: by 10.84.217.30 with SMTP id o30mr15353477pli.351.1507736494679; Wed, 11 Oct 2017 08:41:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507736494; cv=none; d=google.com; s=arc-20160816; b=VAebGXGSKzue7Ru+d6EDp4GXhKJI9aPqru9I02Ig/9gPJAMlIOC3P7ZBuKxR5ELaoK Uc0AZ89EMab6Qq8vSJOB0pKwhvP8Yp7xvG3/3Ka8qKCvtePdu1yB36hP+8cERm8deplD BhyVaaJ/48WZhB8I0p2sAqB7iiqw5aEdLzC7Us80DA1xeEUZTAOBLufvnfSpUVTjvw11 LMHyfeUJx4DcrhO8S8VQK29v1qtowXrs3Uc2CXRfjOKgCUCjyEmu0woybvu5B9xMnEyK r2einYuHBniq67BdJ5Yu7LAY4FNOWex4dv0n2MDFIkLOgvFN5AnZkMggb8xWF8dPqJpQ mohQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=RzRNhrPrApVwo6vJMOG0ZWtzMAa7K8HvOZxIlsuS8V4=; b=MeWycAp91ufxhmzrMiLjcFUwPiP82usz10SvPA2C3hMZ8GPjRZqORCRMKSwn6g0taT Ck84P/K1VfAnLkhnWp1r6XMcrl6YPZB7ItrZHoUO7znhe0C5+ZTVOZSmEzXsuzaOSzJq 2KH/eSSYLH+B2f0ZjJnTXwcOYY4cWc8R1evWUyNCsFoPu03K0OwYgnGpFkeV3YO69Pod oO7r5Qq+XhlOO5cVUkjMh0yqFR4ctG2UZ7ilFwlqYKeSKUUt0pF6BGszhZV8hy760hAe Q9xqUybiLZO7kK/v1BRWLaXZBnkit3yuINyOlCtSrSmrlTJtrbnVa5f8S2ozvhpvFypa EaTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@docker.com header.s=google header.b=YGnDxSIe; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=docker.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g16si184734pfd.472.2017.10.11.08.41.20; Wed, 11 Oct 2017 08:41:34 -0700 (PDT) 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=@docker.com header.s=google header.b=YGnDxSIe; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=docker.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757407AbdJKPkO (ORCPT + 99 others); Wed, 11 Oct 2017 11:40:14 -0400 Received: from mail-it0-f45.google.com ([209.85.214.45]:57052 "EHLO mail-it0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757254AbdJKPkM (ORCPT ); Wed, 11 Oct 2017 11:40:12 -0400 Received: by mail-it0-f45.google.com with SMTP id g18so3339373itg.5 for ; Wed, 11 Oct 2017 08:40:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=docker.com; s=google; h=from:to:cc:subject:date:message-id; bh=RzRNhrPrApVwo6vJMOG0ZWtzMAa7K8HvOZxIlsuS8V4=; b=YGnDxSIeSgmoVeWqc9WBUiSqXH4v/g04gYsroEeO5m7SrdlvfhCpL/tHaqIIsvAXYl 59X5BWBMdSLIe9/E2uPz+48xPfYwvyZKinvQwaEXcS/OeitiKbjosgJnBSEEFKal7BD5 ipxKOuJl7oEnTpRAuDHCFHHPQDl2/Zka9Ln0I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=RzRNhrPrApVwo6vJMOG0ZWtzMAa7K8HvOZxIlsuS8V4=; b=MP94PNpR56MH9RTcfKYuBaprRKOOo0r57mFklFEqIaF8DFu7whvJT6cXceZZC1hqOn SmSIZVgG/RAwyl6vQc7mBHj6jlVKDZTgHhoveRoKr6iOlBafgO1Her+2xcluVp1DyhdO QzhzCCji0Tndhk5yR4dJxBEAFUzbSF69tgvtq8GYYKI07bi7F36x2rF6UEum4jceOn9H OUavMPhRV9GmdhsPgmH4+op+UdKPVYeHbAWJumkZ+sqY+vSICAmn5MozrdqLV/H44JBd ww80Esakfr+eWX8Zp63r9jhr9eXmNg2iafQx3tM/+Eo+pISNaW23cHScZOtB4AangtJt 9mVA== X-Gm-Message-State: AMCzsaXV4Q4U+HMM1bdI/s3YT9OAb0uMuUGOhMCBa/zbzlQdzYbm/QOI plNu+JDQ9t7FNRdNihuypgP9hOIySZA= X-Google-Smtp-Source: AOwi7QCBFaMzYQURZjWPVIlupX0pZEfJ9647HLzIqZlD1Y3hLngTU8fXwvCSOTrwg61zuPxkyOtoFA== X-Received: by 10.36.1.136 with SMTP id 130mr23464690itk.119.1507736411457; Wed, 11 Oct 2017 08:40:11 -0700 (PDT) Received: from localhost.localdomain ([8.24.24.129]) by smtp.gmail.com with ESMTPSA id i19sm6241202ioi.38.2017.10.11.08.40.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Oct 2017 08:40:10 -0700 (PDT) From: Tycho Andersen To: Kees Cook Cc: linux-kernel@vger.kernel.org, Tycho Andersen , Andy Lutomirski , Oleg Nesterov Subject: [PATCH v3 1/2] seccomp: hoist out filter resolving logic Date: Wed, 11 Oct 2017 09:39:20 -0600 Message-Id: <20171011153921.20731-1-tycho@docker.com> X-Mailer: git-send-email 2.11.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hoist out the nth filter resolving logic that ptrace uses into a new function. We'll use this in the next patch to implement the new PTRACE_SECCOMP_GET_FILTER_FLAGS command. v3: * significantly revamp get_nth_filter logic (Oleg) * rebase on 4.14-rc4, using the new __{get,put}_seccomp_filter primitives Signed-off-by: Tycho Andersen CC: Kees Cook CC: Andy Lutomirski CC: Oleg Nesterov --- kernel/seccomp.c | 77 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index bb3a38005b9c..2e1568261ac4 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -977,49 +977,68 @@ long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) } #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE) -long seccomp_get_filter(struct task_struct *task, unsigned long filter_off, - void __user *data) +static struct seccomp_filter *get_nth_filter(struct task_struct *task, + unsigned long filter_off) { - struct seccomp_filter *filter; - struct sock_fprog_kern *fprog; - long ret; - unsigned long count = 0; - - if (!capable(CAP_SYS_ADMIN) || - current->seccomp.mode != SECCOMP_MODE_DISABLED) { - return -EACCES; - } + struct seccomp_filter *orig, *filter; + unsigned long count; + /* + * Note: this is only correct because the caller should be the (ptrace) + * tracer of the task, otherwise lock_task_sighand is needed. + */ spin_lock_irq(&task->sighand->siglock); + if (task->seccomp.mode != SECCOMP_MODE_FILTER) { - ret = -EINVAL; - goto out; + spin_unlock_irq(&task->sighand->siglock); + return ERR_PTR(-EINVAL); } - filter = task->seccomp.filter; - while (filter) { - filter = filter->prev; + orig = task->seccomp.filter; + __get_seccomp_filter(orig); + spin_unlock_irq(&task->sighand->siglock); + + count = 0; + for (filter = orig; filter; filter = filter->prev) count++; - } if (filter_off >= count) { - ret = -ENOENT; + filter = ERR_PTR(-ENOENT); goto out; } - count -= filter_off; - filter = task->seccomp.filter; - while (filter && count > 1) { - filter = filter->prev; + count -= filter_off; + for (filter = orig; filter && count > 1; filter = filter->prev) count--; - } if (WARN_ON(count != 1 || !filter)) { - /* The filter tree shouldn't shrink while we're using it. */ - ret = -ENOENT; + filter = ERR_PTR(-ENOENT); goto out; } + __get_seccomp_filter(filter); + +out: + __put_seccomp_filter(orig); + return filter; +} + +long seccomp_get_filter(struct task_struct *task, unsigned long filter_off, + void __user *data) +{ + struct seccomp_filter *filter; + struct sock_fprog_kern *fprog; + long ret; + + if (!capable(CAP_SYS_ADMIN) || + current->seccomp.mode != SECCOMP_MODE_DISABLED) { + return -EACCES; + } + + filter = get_nth_filter(task, filter_off); + if (IS_ERR(filter)) + return PTR_ERR(filter); + fprog = filter->prog->orig_prog; if (!fprog) { /* This must be a new non-cBPF filter, since we save @@ -1034,17 +1053,11 @@ long seccomp_get_filter(struct task_struct *task, unsigned long filter_off, if (!data) goto out; - __get_seccomp_filter(filter); - spin_unlock_irq(&task->sighand->siglock); - if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) ret = -EFAULT; - __put_seccomp_filter(filter); - return ret; - out: - spin_unlock_irq(&task->sighand->siglock); + __put_seccomp_filter(filter); return ret; } #endif -- 2.11.0 From 1585267907961879709@xxx Tue Nov 28 00:34:48 +0000 2017 X-GM-THRID: 1584805791154205642 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread