Received: by 10.223.185.116 with SMTP id b49csp1049056wrg; Fri, 23 Feb 2018 11:00:11 -0800 (PST) X-Google-Smtp-Source: AH8x227RKjLpDBQX0R4fDCYnHvIU6OXM3irXdwRCMWzkigp5DUVZaYXOZS6Mpu3+PzdgDV8NwV81 X-Received: by 2002:a17:902:47:: with SMTP id 65-v6mr2549046pla.194.1519412411484; Fri, 23 Feb 2018 11:00:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519412411; cv=none; d=google.com; s=arc-20160816; b=Sp1+7IZ/X2nIcWC5KiRBvMM42IOUnczPPDZ1VwTx2M6+YZI19VUZQkRoDSeI6nCMCG F/mJmKHrOtCBfmrjVtPR9d6g8TWEQIa69xuvOiUGtjPwC3vZ5ORmRIhpwtYjvLL/zQ95 U441aSeARxfNpIjV4vUImBW9SmhMOz1UQt8I9jxJFQWxX/YBJF34JpYjgzwgQc9UHgjB ybYt4bRj9sUzmaiiLQwgQmY4H62JfOhIMtahyNL4Z4GWm2G39UHzd3yh9BvShrDf58eW cu0cr+kWs9aIdi0FVxYE6zOa2NHAefK+LenY1/iWt1+hXnaHbI5orOkghZcpdUjSg9IP S04w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=elhcKPbeOxmSl7/QH3MFBIqtw8woLl9QpcS8Ac+f8+g=; b=RxaVjTcKnkxaieQpLxq0C6h/61JobOIIfHbu8sKY6eoM9QVBd+inuk2dekWQrWUgRM L78e/XitkncDN8S1NrDE4NbzqxRobtM06Vc6vpaPgVmaPT/AAztu2gDSwHmw4TIUHszq h/PRW4MS4OY11tc0tO4ndIs394vmmWVmZPcVBnyi/h2eIcxqRlnVPqltx+MbA+7GL1ZW +8qmJULkkZL6ky6bPxHDdAVZkF52xid66ipQRUF2EZXSBOf9K3+TK4wiHDKasmMMiJoi Id3h6nNu0GClVD1nFnFUSuPDtxAsLFR8EfmKrWnibp+GbkObh56gUdCDth1jO0DP6WwL lWUg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g6-v6si2117301pll.745.2018.02.23.10.59.57; Fri, 23 Feb 2018 11:00:11 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965518AbeBWS57 (ORCPT + 99 others); Fri, 23 Feb 2018 13:57:59 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:50352 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965490AbeBWS5z (ORCPT ); Fri, 23 Feb 2018 13:57:55 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 7E9C5107C; Fri, 23 Feb 2018 18:57:54 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Paul Moore , William Roberts Subject: [PATCH 4.15 04/45] selinux: ensure the context is NUL terminated in security_context_to_sid_core() Date: Fri, 23 Feb 2018 19:28:43 +0100 Message-Id: <20180223170715.973981040@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180223170715.197760019@linuxfoundation.org> References: <20180223170715.197760019@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Paul Moore commit ef28df55ac27e1e5cd122e19fa311d886d47a756 upstream. The syzbot/syzkaller automated tests found a problem in security_context_to_sid_core() during early boot (before we load the SELinux policy) where we could potentially feed context strings without NUL terminators into the strcmp() function. We already guard against this during normal operation (after the SELinux policy has been loaded) by making a copy of the context strings and explicitly adding a NUL terminator to the end. The patch extends this protection to the early boot case (no loaded policy) by moving the context copy earlier in security_context_to_sid_core(). Reported-by: syzbot Signed-off-by: Paul Moore Reviewed-By: William Roberts Signed-off-by: Greg Kroah-Hartman --- security/selinux/ss/services.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c @@ -1413,27 +1413,25 @@ static int security_context_to_sid_core( if (!scontext_len) return -EINVAL; + /* Copy the string to allow changes and ensure a NUL terminator */ + scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags); + if (!scontext2) + return -ENOMEM; + if (!ss_initialized) { int i; for (i = 1; i < SECINITSID_NUM; i++) { - if (!strcmp(initial_sid_to_string[i], scontext)) { + if (!strcmp(initial_sid_to_string[i], scontext2)) { *sid = i; - return 0; + goto out; } } *sid = SECINITSID_KERNEL; - return 0; + goto out; } *sid = SECSID_NULL; - /* Copy the string so that we can modify the copy as we parse it. */ - scontext2 = kmalloc(scontext_len + 1, gfp_flags); - if (!scontext2) - return -ENOMEM; - memcpy(scontext2, scontext, scontext_len); - scontext2[scontext_len] = 0; - if (force) { /* Save another copy for storing in uninterpreted form */ rc = -ENOMEM;