Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7972664ybi; Thu, 6 Jun 2019 04:44:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHgXQYtdOOUGDUVJimSADCD9OkSjGKEpspmwgbS0MVuvXaHkmMPhTxsj2e/qKUoQLjADFF X-Received: by 2002:a65:6096:: with SMTP id t22mr2930294pgu.71.1559821479140; Thu, 06 Jun 2019 04:44:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559821479; cv=none; d=google.com; s=arc-20160816; b=yd1276UD7GR7lo+jRlRaBOwJ+nibTya5qZ9gvpzqR+cMbXWUGQLZ/zoWzHKDV/67KT EWNfh/nSGPZrENw8WYMkeiMi9ZzY7bt4tdfVAlhm14m9rTaMZ3dCRFdLDVxS8DbdcQfm l1XElmmIWL1kRT+oxJSzAL4nZ8XXmjixOS1WcDzHMDGjr3oIvKD4N5CVfjG/NgkKfAY4 RnX6SYiX4NQHD9L4oHVu5z3wst++6iMQ0RTEon8IxIN1QXVXdyEDoDekKv780CmxfxZ3 Py4v8FjntZgor5qAdeBB65jwGYYU2ocSl1UYuITKiBrI14Cb5awzTulStPHa40GyfHY5 T9Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=1/TKE/QmlgGH1FFbgu8/s1F6UVtd/Yfiy+F2mc9SqpQ=; b=Sw9qh+EfQLSn0Iz4FJbUkI5fKQHCiNJN08b+BT7oOnElOAYGLZATHSUyvbIzuDR/ch 5ZKUtwiR+S+kIDF7KJKB9OXREy370E2ybvRsawZ4yBSSvtymbWPV4LoJ6Sz19ohevoz5 J2Jq2VrGB5Vm47NQRrBY5hkksMA907k2NYnKc9mdrSophlNv2F7e533NxoI2SSjeG1En DEsbkcU5atWW8AWSdpGM9iTM1jT7lpzIqykVGFNVzwJmpwas5nxY9YxLi8ElWKzvADNK HMuWZf4aq0Enjtx9VQB22kOPmcaaKvPtOgjKm3A29stIUHXqmldpeBltj5vluBRIlNqP 2QKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lPDkSTVe; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h5si1912214pgc.8.2019.06.06.04.44.22; Thu, 06 Jun 2019 04:44:39 -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=@gmail.com header.s=20161025 header.b=lPDkSTVe; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727515AbfFFIzd (ORCPT + 99 others); Thu, 6 Jun 2019 04:55:33 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:55317 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727279AbfFFIzd (ORCPT ); Thu, 6 Jun 2019 04:55:33 -0400 Received: by mail-wm1-f66.google.com with SMTP id a15so1227498wmj.5; Thu, 06 Jun 2019 01:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=1/TKE/QmlgGH1FFbgu8/s1F6UVtd/Yfiy+F2mc9SqpQ=; b=lPDkSTVeuBYLJ/kHz0jbAmaDqBJE2OPHrYYUwCgZE6l8zE1YHN+vLSe2UegG1UW/yY xnJMAAV6BnzK1GZ+t6r4u0AEvH2+okmVbHxjcfb9OvGETwGbwDJEBZRf9R/lNf6qtZlg BPnjUevyK22LpLZwLbkZzqIM0yBfPi9hfrbWZBwfY9De5UMfI/+UckNmaVSlLYmkrj7E omwCs8EVNrqlplW2/vRGowMeSFyA+Shobq7O5D77ORVqkhfh3h+c1WmNEDcggXc2dIFw NoiPLnJcpxbU92zhuu+4kruD2YBlx+uLFruFzd/Hqa/0m3vbQnUlp/AqukwEvzOPD9R+ e4OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=1/TKE/QmlgGH1FFbgu8/s1F6UVtd/Yfiy+F2mc9SqpQ=; b=EVt2QLsymmKW+Q6RpR+1zviHUBTGAb6u2HpaBxayvechr7HL45XG9hdF9JOuQJd57x 9UY43tAbTWyNmtlkfbPt1PQack8apPGqL2bXbDOk+tEaDUa5O0Sk5QHIrYC4T9q3texm Ro/RPtZLZSoUWuyiEjWstDAgXXOEgiBDNVtHfeMa0JYLRmfViFo373U2P5NpHigOG3xa bCu2/vcRi4yMHcNgDD/lSP67vN3w0QaAUDQc2kFkSXhswVD1Nt7vyeX0iNGzDoqu+bMW qhZvBMlHN4vl420HgYgtjkqhcnIj5AEuwlGefJc0ca+vW5wT6m/4sCtsFU8ZUBTPaXjy rDRg== X-Gm-Message-State: APjAAAXLUnRudPg/ryuKdAiDV6W+RRAbEzsUBYW0Jy5vPFJu7Ezteal3 B1cgEkbq5C54bI1d3rnhE59mMniq X-Received: by 2002:a7b:c94a:: with SMTP id i10mr22767614wml.97.1559811331099; Thu, 06 Jun 2019 01:55:31 -0700 (PDT) Received: from zhanggen-UX430UQ ([108.61.173.19]) by smtp.gmail.com with ESMTPSA id r131sm1077565wmf.4.2019.06.06.01.55.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Jun 2019 01:55:30 -0700 (PDT) Date: Thu, 6 Jun 2019 16:55:24 +0800 From: Gen Zhang To: paul@paul-moore.com, sds@tycho.nsa.gov, eparis@parisplace.org Cc: selinux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] selinux: lsm: fix a missing-check bug in selinux_sb_eat_lsm_o pts() Message-ID: <20190606085524.GA21119@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In selinux_sb_eat_lsm_opts(), 'arg' is allocated by kmemdup_nul(). It returns NULL when fails. So 'arg' should be checked. And 'mnt_opts' should be freed when error. Signed-off-by: Gen Zhang Fixes: 99dbbb593fe6 ("selinux: rewrite selinux_sb_eat_lsm_opts()") --- diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 3ec702c..13479cd 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2616,10 +2616,11 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) char *from = options; char *to = options; bool first = true; + int rc; while (1) { int len = opt_len(from); - int token, rc; + int token; char *arg = NULL; token = match_opt_prefix(from, len, &arg); @@ -2635,15 +2636,15 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) *q++ = c; } arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); + if (!arg) { + rc = -ENOMEM; + goto free_opt; + } } rc = selinux_add_opt(token, arg, mnt_opts); if (unlikely(rc)) { kfree(arg); - if (*mnt_opts) { - selinux_free_mnt_opts(*mnt_opts); - *mnt_opts = NULL; - } - return rc; + goto free_opt; } } else { if (!first) { // copy with preceding comma @@ -2661,6 +2662,12 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) } *to = '\0'; return 0; +free_opt: + if (*mnt_opts) { + selinux_free_mnt_opts(*mnt_opts); + *mnt_opts = NULL; + } + return rc; } static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)