Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2210942ybk; Mon, 11 May 2020 15:03:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfYbwer9ogMgpoxTOT143R5MUM+Y+QzSwqly1U6JoEbcM4BoKtnLxk4h15ESIwA/s8+c1L X-Received: by 2002:a17:906:3495:: with SMTP id g21mr3685014ejb.154.1589234606704; Mon, 11 May 2020 15:03:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589234606; cv=none; d=google.com; s=arc-20160816; b=M8jAznfvHJf38VuYZ0Q2hbxjgFWoPVQ+MhwkfFZGltxT4Scv6gfV8JDUOdBvGyRAJr 4x3b1iwKyQYMdJYV1HJOCjnuGY5sRCzGtWBluFl+lC2bmMMY93nePXldFoBHe7W+IAxN HRICkgrcrv1mY9NPMDuHmHb2gGZ/56qYS1wcJVoQ3z8UrVSyPMxtXtxEd0imXxzO9wfi YeeK7aQG47iavgGRJyTo8R5E3b1RkrdNRPvlo1uSPuV7t0RdHEAZX1WrmWznz27O77NT VIswN0jA9EFjPHeK4YFeOFOHtFg8a33CAJHRjd7SsHPs4Za4St1ZIBPdmLoFZDfIM4jh XunQ== 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:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=lG05Rn0O+ELWikk9dZBI+YfJaiuIbJQEERVy0w4Y7tg=; b=vcU+gmMKTPvr0IsaBfzAEyvrNVtwjg6tYFDgt+Q+PbvmRU9Jp/RTzaO8IGK1Ch1Atc SlkLl/OGMMtI7Jiek5Z+YZeaaMAF0RFb0WsFYdeQh5MxkSpIMR0EsCtdDLI3w16691XO WIG2WQ3mh4P5wif8wKAb2MpVMBZ/oiJS6tT4IxyBjg+x3AtgPvmA/4SIYHf28Omql4fL d7zRVMJDiPciWCr6NxEfJg6FlzGVnJabEZdfjsm53oq/Ho3XYERPLkfMEOKiY0TFfGJC mqCZ/cqvaV94unuSstnq0GJVahucu6Qn8MZow+pLDqhrT+eHuXgmZK4GKKPF+270Rc1x 1RnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=io1tk0Tq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x25si6745687eds.136.2020.05.11.15.03.00; Mon, 11 May 2020 15:03:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=io1tk0Tq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728040AbgEKV7N (ORCPT + 99 others); Mon, 11 May 2020 17:59:13 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:50838 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727835AbgEKV7M (ORCPT ); Mon, 11 May 2020 17:59:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589234351; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=lG05Rn0O+ELWikk9dZBI+YfJaiuIbJQEERVy0w4Y7tg=; b=io1tk0TqFvFfIqkf3VjPVBy76av9HQaTz/R20worBO71xUArtKBOw11pEDzURhOc6OYC9e VU0aULnO3adoEZLVohKDwbavWQrP5Snq1zvljkg+NdaX7lL+0h03baycSo5umPcVdU8LbH leYFOSU2GnerONzdOLU2NzK8HhExQrM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-426-igxqb0uCNjCy1dgbOX5KDw-1; Mon, 11 May 2020 17:59:10 -0400 X-MC-Unique: igxqb0uCNjCy1dgbOX5KDw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ED3E281C9A7; Mon, 11 May 2020 21:59:08 +0000 (UTC) Received: from optiplex-lnx.redhat.com (unknown [10.3.128.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C18638DE2; Mon, 11 May 2020 21:59:07 +0000 (UTC) From: Rafael Aquini To: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com Subject: [PATCH] kernel: sysctl: ignore invalid taint bits introduced via kernel.tainted and taint the kernel with TAINT_USER on writes Date: Mon, 11 May 2020 17:59:04 -0400 Message-Id: <20200511215904.719257-1-aquini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The sysctl knob allows any user with SYS_ADMIN capability to taint the kernel with any arbitrary value, but this might produce an invalid flags bitset being committed to tainted_mask. This patch introduces a simple way for proc_taint() to ignore any eventual invalid bit coming from the user input before committing those bits to the kernel tainted_mask, as well as it makes clear use of TAINT_USER flag to mark the kernel tainted by user everytime a taint value is written to the kernel.tainted sysctl. Signed-off-by: Rafael Aquini --- kernel/sysctl.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 8a176d8727a3..f0a4fb38ac62 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -2623,17 +2623,32 @@ static int proc_taint(struct ctl_table *table, int write, return err; if (write) { + int i; + + /* + * Ignore user input that would make us committing + * arbitrary invalid TAINT flags in the loop below. + */ + tmptaint &= (1UL << TAINT_FLAGS_COUNT) - 1; + /* * Poor man's atomic or. Not worth adding a primitive * to everyone's atomic.h for this */ - int i; for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) { if ((tmptaint >> i) & 1) add_taint(i, LOCKDEP_STILL_OK); } + + /* + * Users with SYS_ADMIN capability can include any arbitrary + * taint flag by writing to this interface. If that's the case, + * we also need to mark the kernel "tainted by user". + */ + add_taint(TAINT_USER, LOCKDEP_STILL_OK); } + return err; } -- 2.25.4