Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3752841imm; Mon, 18 Jun 2018 03:32:34 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKlu1mCxlbzGt+DfnJg5Cq26rajjP6R4NrJ/c5Qbs3l9+qUrgr04TDcIy0CBHzYackZOWXM X-Received: by 2002:a65:6390:: with SMTP id h16-v6mr10591460pgv.382.1529317954291; Mon, 18 Jun 2018 03:32:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529317954; cv=none; d=google.com; s=arc-20160816; b=GWMyrenAuyFAeZbablEfig+J9nl2R8Ukz4qoqAECd7/Olmnj/W0FEYZ6FkoG7UbKCR 7HT9PHlMAa/xTaXvZDPbzZ6cLcySnKDl5fRavlR4M9spglBZDN6PG6r5U7/ZR9P/V9ra vhWf+x3pnMeDAUaqRbHC0XxJqt+Lq8c/22vqfhHjpcVqhQ1hGsPvdP6zPDlaimiEJJUf eOYucTZWOGgeVcw8OFuOwhexxCd9BlHfTc7xAUxzZ6DSys3BZKUWxOinH0nagliAFlEo gLfiq0mn0IsKrQNtXzSSh1F0ROT3PnKlUCXblK9FXr/HyESyKRbpK7cpwWWDo7yUAJxa YDFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=QXO5htvcrcM+kBMFjJyGhDbaLyhLjwOaeBOhdDl9VMs=; b=Y+DwEiC0+d64XIuUS5t10ZvomCE1E07xcuHE5Ml5IjFNs3eCpxR1BQoZj7seLOZHVW MM8okNPmuns5EqLSZvBYKdLgrqa2z3MI6eg0MJ+1aEJyJ1FXZemAOPuT4JEMN8hkpY8K WxG/08IRXQSS9xaUZYIYYCdtG0wembqyNtr3HSalwFKJQ3s0g5USze6/lcFj0KMB0WCK fx4OvXqLcrkpDd4cKKeyNrnlErdEAVuku51F5J2DC0M3FnzerJc7O/2wc2tBz+UCABEi BKVorpdg8yu29xjNduZLc6bTLo9Ch2H+nRv7j3cfGr0TcqW2KGDzN2J81aayeZeBRV7o z/IQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r9-v6si11700885pgf.664.2018.06.18.03.32.20; Mon, 18 Jun 2018 03:32:34 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S937182AbeFRKaC (ORCPT + 99 others); Mon, 18 Jun 2018 06:30:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48492 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933600AbeFRK2y (ORCPT ); Mon, 18 Jun 2018 06:28:54 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0305130820C0; Mon, 18 Jun 2018 10:28:54 +0000 (UTC) Received: from llong.com (ovpn-116-106.phx2.redhat.com [10.3.116.106]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1660D5C26B; Mon, 18 Jun 2018 10:28:50 +0000 (UTC) From: Waiman Long To: "Luis R. Rodriguez" , Kees Cook , Andrew Morton , Jonathan Corbet Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, Al Viro , Matthew Wilcox , "Eric W. Biederman" , Takashi Iwai , Davidlohr Bueso , Waiman Long Subject: [PATCH v8 2/5] ipc: IPCMNI limit check for semmni Date: Mon, 18 Jun 2018 18:28:15 +0800 Message-Id: <1529317698-16575-3-git-send-email-longman@redhat.com> In-Reply-To: <1529317698-16575-1-git-send-email-longman@redhat.com> References: <1529317698-16575-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Mon, 18 Jun 2018 10:28:54 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For SysV semaphores, the semmni value is the last part of the 4-element sem number array. To make semmni behave in a similar way to msgmni and shmmni, we can't directly use the _minmax handler. Instead, a special sem specific handler is added to check the last argument to make sure that it is limited to the [0, IPCMNI] range. An error will be returned if this is not the case. Signed-off-by: Waiman Long --- ipc/ipc_sysctl.c | 23 ++++++++++++++++++++++- ipc/util.h | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ipc/ipc_sysctl.c b/ipc/ipc_sysctl.c index f87cb29..49f9bf4 100644 --- a/ipc/ipc_sysctl.c +++ b/ipc/ipc_sysctl.c @@ -88,12 +88,33 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); } +static int proc_ipc_sem_dointvec(struct ctl_table *table, int write, + void __user *buffer, size_t *lenp, loff_t *ppos) +{ + int ret, semmni; + struct ipc_namespace *ns = current->nsproxy->ipc_ns; + + semmni = ns->sem_ctls[3]; + ret = proc_ipc_dointvec(table, write, buffer, lenp, ppos); + + if (!ret) + ret = sem_check_semmni(current->nsproxy->ipc_ns); + + /* + * Reset the semmni value if an error happens. + */ + if (ret) + ns->sem_ctls[3] = semmni; + return ret; +} + #else #define proc_ipc_doulongvec_minmax NULL #define proc_ipc_dointvec NULL #define proc_ipc_dointvec_minmax NULL #define proc_ipc_dointvec_minmax_orphans NULL #define proc_ipc_auto_msgmni NULL +#define proc_ipc_sem_dointvec NULL #endif static int zero; @@ -175,7 +196,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .data = &init_ipc_ns.sem_ctls, .maxlen = 4*sizeof(int), .mode = 0644, - .proc_handler = proc_ipc_dointvec, + .proc_handler = proc_ipc_sem_dointvec, }, #ifdef CONFIG_CHECKPOINT_RESTORE { diff --git a/ipc/util.h b/ipc/util.h index 0aba323..8e9c52c 100644 --- a/ipc/util.h +++ b/ipc/util.h @@ -218,6 +218,15 @@ int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, void (*free)(struct ipc_namespace *, struct kern_ipc_perm *)); +static inline int sem_check_semmni(struct ipc_namespace *ns) { + /* + * Check semmni range [0, IPCMNI] + * semmni is the last element of sem_ctls[4] array + */ + return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > IPCMNI)) + ? -ERANGE : 0; +} + #ifdef CONFIG_COMPAT #include struct compat_ipc_perm { -- 1.8.3.1