Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp130197rdb; Wed, 14 Feb 2024 15:24:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVGGFowtdQB76CAGRfgcgzZ3Yz1ZATIny5H4uqQNPydOtTSlk5UYCKuZXVoFgPCV5yW3vSiCsCVvYQAbFrydEofIGk6uoPsFiCFjSOdTA== X-Google-Smtp-Source: AGHT+IEO2NihNwxkFO/HJ9yxvQ9oGw9ZMzC3JU+/jfdK+59zbuKcas2kSpYmG8I9m8BsgxtK60vY X-Received: by 2002:aa7:d58e:0:b0:560:9267:95af with SMTP id r14-20020aa7d58e000000b00560926795afmr55428edq.22.1707953094851; Wed, 14 Feb 2024 15:24:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707953094; cv=pass; d=google.com; s=arc-20160816; b=M6QDVMcd82ozPSZHxh65jnBX/d8nQZebpuDFQ/ajzi5CrmVvrXe/RVzJe2zY29oOmH O5/ZB3PMpQayuvInbsPN3qBfej4WDGkii8CeWfHOIyTQRMdAystq0YaCSrxEnMImUxc8 uZDBu1LBkstCKk3ZKyq8FvG7RXKfO/s/XRzeaUOK02CptUCLa0DkYqps+jLAZEq113iE cgV7nef1lFtDvcHK3JL777+p7qpcM1absBEDaxPxhRMmJN3JzqCSpQS5kOB7Z8dLt5nu cqakcOAg+9/cFRhhhyWH10lA7oaXH1+TOTzp2BMTmTzo5yZr/YnZEQwsUefThxhwzGRL JXWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ZOQlVf5qnnKk5eJkb2R3QyzFtOwnY+yrwTa/skPqH2g=; fh=n11J0Zb8VTp4Sndvg4OLXDtDSFrS0IsiOK8MJCCMEEo=; b=zefkHx+UXHLZvKPAHdxdxHNlxVTDXS8xEq/5fcIODio0c9rR0DmNDYNZl/vuEmmOoR jTNVH3R1f/aUzw9rtcDzZL4M+V8dhRKGxRzn6z6fyVAK9vNe61SZZICXAlsuKRrqZQYA Mq9oEBOMjlsjWp3+l1nW/k4MeheLkG7IKrw6fZFdCLsEC1h46FfNU3Wbd+KeYAacRpO4 UW3naqgmTQVvDRdy6slK4djm5Ekqw8j7Az5ZmB3ZNis5S5GMUgu/PYcXrBx05nBkv+f+ OS/Xz3WUQT87le421PlOv6LUq5IfmpJxhFL63a9rDKys1EtZ6bJksTJoMjbrBoDOVGrY AvEg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Bci9uY+N; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-66119-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id de5-20020a056402308500b0055deaaf8b06si6835edb.346.2024.02.14.15.24.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:24:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66119-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Bci9uY+N; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-66119-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8F5681F2A47D for ; Wed, 14 Feb 2024 23:24:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 975111419AE; Wed, 14 Feb 2024 23:24:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Bci9uY+N" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DC4A1DDD7 for ; Wed, 14 Feb 2024 23:24:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707953085; cv=none; b=hF+s2nYrMx2JvpVSMuHcCZaY3InUdKncfd1eiKnoDZJPbJap4BI13Eef7nU1xADmRoSJXEqgObJP07FIJjHuafwkfZZ3T14IBdYDNVf6eDWSM1AXlEhMHoG+UGx/Qrp1E5gCl/NYs7lIpHLb5zDqZNlh8wVxiJC1VZdP9r9IL4g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707953085; c=relaxed/simple; bh=z3FV6SculjJxKoq8cb1i2S4q1XbiP+WG8CbFW/oAXq4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pvyjqglAVENE+14kNtR9ak+Mgz5E2TXyAYV9LvE9qA4ByijUOTTTopA/JHv/P17TCNCmpa1PTlljGFgbk1lownUGgKlSjxIrYfzksEdA6wVh5v/0AeRp+8S/4Q2U1ZzIRADKjtb7vydOd5pQVPc4+q/1JYb0rhhKTFjjXjdnMzs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Bci9uY+N; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6e08dd0fa0bso296044b3a.1 for ; Wed, 14 Feb 2024 15:24:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707953083; x=1708557883; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ZOQlVf5qnnKk5eJkb2R3QyzFtOwnY+yrwTa/skPqH2g=; b=Bci9uY+NRjdfbwxOM8ynNOWIcM3AwfJmNJwABSWS+BQTXyW7r1f0UYcgdrPXS/k2lz qGeL3eTnQc5pQq3pFRrZXXUd47Jmx6J6K96TNxCAqJ5HDy4z69D7czKsK4zrnrgVg84E /7D5ea1F1YRDqjf2WjIN1pic+bKmV4t+o717w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707953083; x=1708557883; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ZOQlVf5qnnKk5eJkb2R3QyzFtOwnY+yrwTa/skPqH2g=; b=N+Jw6heF4dRNWEdtqMwlA91oIbWf7y0v0kiM4AJMHqyfYrmimFzPLidGKuhxFyz3Jt fEm1t57qEX/Rtymc9oG4U65YbrUvlNJdVffEjXJfP3oqk+ULxDpPlxcaCASTn8ibn4ku P8DbULhvab1aUPjjjOEVi/2Gwzn9wjQZy/bEPPf8kivcrVILnVOVry8FQ96FtCmK1BSI tDG+Vc3xtTpFTjzMD6C55weTo8e8Q/D9CE7rcgT4ywFCWWn9vtkYdIU89kCHhgQ2IP4v 9YtR5sBjYI5Z2P3MB11cCvQKhnhD9xtAI435w5tiNyMkzTMMVCxuZ1uxLhlKjMowsEnM YNAw== X-Forwarded-Encrypted: i=1; AJvYcCUK8vq7E7NZeXD3w+3MFrygA/7y77Xp+M7uKEocfovE3tRqgYyADk14R4QJdA6lqxec0EF22E68oJxyS59zwztCIoZMQQWxpc2nIAMb X-Gm-Message-State: AOJu0YzGW98sXP1uF+Gohkymq6nFQPMCPh0VY/jkSNiJ8gqvkAShgpbr pZW9pgi53pLkIlNTkDJ2ddiOb4g1cqekoWxXPC95oFhPrs492bXVnOvjze4Txw== X-Received: by 2002:a05:6a00:1803:b0:6e1:eb1:cc8f with SMTP id y3-20020a056a00180300b006e10eb1cc8fmr4109107pfa.4.1707953083534; Wed, 14 Feb 2024 15:24:43 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUj0vkPjui6LzJeZojNrXuDHzO6EZj4cwuyR4iuh6X2ksCiiEw0V4Y06/EH9T6pIPIttLPSnuvYtpm6Ee237lpSnQBrwdCUiPDLoWs9nFXwkp/pOl4nJ0C+q60EYAZWs5z8/W0H4lXTEXHxXoYn7kLkoC4/BikYH5JP465O8YciGPyA+Nq32SqR5H6o/9WioHN7Ay6ZjEKsr9KAcKjEMDXX9zyQUsZlivfx4DKlf1U6MwP8OU8ws0qA33DcaLU3iJIPKkp5N3b7kOSmi0vO Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id y18-20020a637d12000000b005dc36761ad1sm4882021pgc.33.2024.02.14.15.24.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:24:43 -0800 (PST) Date: Wed, 14 Feb 2024 15:24:42 -0800 From: Kees Cook To: Jann Horn Cc: Paul Moore , James Morris , "Serge E. Hallyn" , Casey Schaufler , John Johansen , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] security: fix integer overflow in lsm_set_self_attr() syscall Message-ID: <202402141453.FEC8EF722@keescook> References: <20240214160538.1086089-1-jannh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240214160538.1086089-1-jannh@google.com> On Wed, Feb 14, 2024 at 05:05:38PM +0100, Jann Horn wrote: > security_setselfattr() has an integer overflow bug that leads to > out-of-bounds access when userspace provides bogus input: > `lctx->ctx_len + sizeof(*lctx)` is checked against `lctx->len` (and, > redundantly, also against `size`), but there are no checks on > `lctx->ctx_len`. > Therefore, userspace can provide an `lsm_ctx` with `->ctx_len` set to a > value between `-sizeof(struct lsm_ctx)` and -1, and this bogus `->ctx_len` > will then be passed to an LSM module as a buffer length, causing LSM > modules to perform out-of-bounds accesses. Ugh. Thanks for catching this. I continue to want to get the unsigned integer overflow sanitizer rolled out, which would have caught this. > > The following reproducer will demonstrate this under ASAN (if AppArmor is > loaded as an LSM): > ``` > #define _GNU_SOURCE > #include > #include > #include > #include > > struct lsm_ctx { > uint64_t id; > uint64_t flags; > uint64_t len; > uint64_t ctx_len; > char ctx[]; > }; > > int main(void) { > size_t size = sizeof(struct lsm_ctx); > struct lsm_ctx *ctx = malloc(size); > ctx->id = 104/*LSM_ID_APPARMOR*/; > ctx->flags = 0; > ctx->len = size; > ctx->ctx_len = -sizeof(struct lsm_ctx); > syscall( > 460/*__NR_lsm_set_self_attr*/, > /*attr=*/ 100/*LSM_ATTR_CURRENT*/, > /*ctx=*/ ctx, > /*size=*/ size, > /*flags=*/ 0 > ); > } > ``` > > (I'm including an ASAN splat in the patch notes sent to the list.) > > Fixes: a04a1198088a ("LSM: syscalls for current process attributes") > Signed-off-by: Jann Horn Reviewed-by: Kees Cook > --- > ASAN splat from the reproducer: > ================================================================== > BUG: KASAN: slab-out-of-bounds in do_setattr (security/apparmor/lsm.c:860) > Read of size 1 at addr ffff888006163abf by task setselfattr/548 I'd rather prefer that this splat (or some portion of it) stay in the actual commit log. It makes it easier to scan for sanitizer-related fixes, etc. -Kees -- Kees Cook