Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp887606imm; Thu, 13 Sep 2018 09:12:51 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaAbtwKeOiAqOoZbsdtq/euEuj93M+CoSTTdN8mYw3d5NptHNxnN6rlciny+qkpqG8t3rdw X-Received: by 2002:a63:9246:: with SMTP id s6-v6mr6158176pgn.141.1536855171849; Thu, 13 Sep 2018 09:12:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536855171; cv=none; d=google.com; s=arc-20160816; b=aLn+JtrGdwZdu2H67BrgwQWoj5snbUtqqUvmY+oEKEJlhioFuoIymZtkiMfsIeKHRD YXOpEJSvPM9TPwsBMXZe8GES/8bERQzi0Fe73zMYJ6bmIBKdDcnQAtC6po6QFklvxl3a GhH+EHJp6Z02L4H7h0G0iKFqQWbEQFM3Bz4iNxWya0DeWsid4WRF4an4NZd6Eshi2h4m jk0fH98kseo+98ujJZSnTgu3i05WV1hBt2CGJL9/gB7RaTKcdFQc/z8DfeNJ6WmDrSDp CO2CaQCdUhYr8spK2ziHktaMVu5XD91Xyym0/0TX8OvO/fGc66joZ7n6QcEfMTaSkdiJ vCDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=zGk//fqE+XT9HqJjdclLb8ahsHMYDLzUz4mSW+Kg4lk=; b=adf8SKgf8ekDRDdJQ10RXzAOwPZBvEgfN90ZtSj/ftoJadcCSYu/Usc8IGOI5eXKw0 vvCK4CwP4juhUQyJXdazd3H62Lfyg4n7I59T0zJ/CkwGHhr2RY+SsRzexzaFAWK+gxuA Zf0XjWwuFAc2gqWew2T+m+dfF2l1KS40vodJeXslYAaDUQivBOVoWOMZPxE9uCrxaeZk gtM5o+tpSMW2WHSO5wDmakNEEek2FFD1f78wybONwLaiK6Inh8/SKfHwquWB5drp9dIc B5zHhpqG2pmmhKGBmpR3Ppn8WbrKsZt9I8rKzFAbhrYrL0u7Slsh8isppg+DMt0FCBwW 4n2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=maitDEE9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t23-v6si3721790pgk.26.2018.09.13.09.12.36; Thu, 13 Sep 2018 09:12:51 -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=@google.com header.s=20161025 header.b=maitDEE9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728161AbeIMVWc (ORCPT + 99 others); Thu, 13 Sep 2018 17:22:32 -0400 Received: from mail-qk1-f202.google.com ([209.85.222.202]:54086 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726819AbeIMVWc (ORCPT ); Thu, 13 Sep 2018 17:22:32 -0400 Received: by mail-qk1-f202.google.com with SMTP id f64-v6so5196354qkb.20 for ; Thu, 13 Sep 2018 09:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=zGk//fqE+XT9HqJjdclLb8ahsHMYDLzUz4mSW+Kg4lk=; b=maitDEE90y9ZpFHAvvMn/x1SytBhaKJE5hwOmHROvuWoDrF91leA+Kts1l/tZa12op d/MzxPKgEESRfOrY50abSzYIK3S5cTfTnhzR2s+GuTVelh1kcvk18rM5GpefuQjW0FXX Z0RfOarFMB9GUUKEhc9tSHwAufHMcC/eQ3RGdQ6eQSvKjVvcV3uDb584H6219Wzg/FUL wa/OmCWjcJwRUN0EB25QTl3HkXIEIcwsc3lweUzo+2BWVViWEd1t8auGzkku2Wn5BJ1p bGMi6EcNhGZDNFhYPpQo4sP7+k0fL91y5VfIqiRDAHb1mKAuzFRVVrw+SOU5ohgEKqRz 9atA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=zGk//fqE+XT9HqJjdclLb8ahsHMYDLzUz4mSW+Kg4lk=; b=DM8qaPqkaVxrBClU6jPoXJ//S7DsMXYSE+7NotBQnw4/XsIx5WuwolonJeg5R2hRFn wGu85wrDlCArZssDNVlrAmJMVKavS0ocOJ1Np9PNZWD5YWPyr25CTFjzZrsUkzHw56Wq k6v7r0Kr4R5/HNWsf1X9gXiZLNaVJ4/sWwvxqgr2JdO7359Fz4QXmewxt4LYyly4qeTn UFeBh9XWpCwfSI1TAcG22ByYezeNQfomXtyVktD08X2dtidOvERk/KVgczuoplDB4yjb eAdLvu4/Pbz5W8H/eXIosLK75GLCz8bbKr3E1sPL4HYEXXbLWmz6DNHyuonoErM6D8Eb efOQ== X-Gm-Message-State: APzg51C+Wsxr55oJsko0/9dE7vR7FNPz5Z1vwcOJ7HZUjXeDxPfVbCcZ EsseF8RMsXSCUFRVqUpIras+5YTqIw== X-Received: by 2002:a0c:bd26:: with SMTP id m38-v6mr2462679qvg.53.1536855140660; Thu, 13 Sep 2018 09:12:20 -0700 (PDT) Date: Thu, 13 Sep 2018 18:12:09 +0200 Message-Id: <20180913161209.63693-1-jannh@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.19.0.rc2.392.g5ba43deb5a-goog Subject: [PATCH] apparmor: don't try to replace stale label in ptrace access check From: Jann Horn To: John Johansen , apparmor@lists.ubuntu.com, jannh@google.com Cc: linux-kernel@vger.kernel.org, Kees Cook Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As a comment above begin_current_label_crit_section() explains, begin_current_label_crit_section() must run in sleepable context because when label_is_stale() is true, aa_replace_current_label() runs, which uses prepare_creds(), which can sleep. Until now, the ptrace access check (which runs with a task lock held) violated this rule. Also add a might_sleep() assertion to begin_current_label_crit_section(), because asserts are less likely to be ignored than comments. Signed-off-by: Jann Horn --- I have tested that just the might_sleep() yells a lot, and adding the rest of the patch stops the yelling. security/apparmor/include/cred.h | 2 ++ security/apparmor/lsm.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/security/apparmor/include/cred.h b/security/apparmor/include/cred.h index e287b7d0d4be..265ae6641a06 100644 --- a/security/apparmor/include/cred.h +++ b/security/apparmor/include/cred.h @@ -151,6 +151,8 @@ static inline struct aa_label *begin_current_label_crit_section(void) { struct aa_label *label = aa_current_raw_label(); + might_sleep(); + if (label_is_stale(label)) { label = aa_get_newest_label(label); if (aa_replace_current_label(label) == 0) diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index 8b8b70620bbe..e3f40c20b9b4 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -114,13 +114,13 @@ static int apparmor_ptrace_access_check(struct task_struct *child, struct aa_label *tracer, *tracee; int error; - tracer = begin_current_label_crit_section(); + tracer = __begin_current_label_crit_section(); tracee = aa_get_task_label(child); error = aa_may_ptrace(tracer, tracee, (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ : AA_PTRACE_TRACE); aa_put_label(tracee); - end_current_label_crit_section(tracer); + __end_current_label_crit_section(tracer); return error; } -- 2.19.0.rc2.392.g5ba43deb5a-goog