Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752078AbbF1N2b (ORCPT ); Sun, 28 Jun 2015 09:28:31 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:42373 "EHLO out3-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752725AbbF1N16 (ORCPT ); Sun, 28 Jun 2015 09:27:58 -0400 X-Sasl-enc: yDpVAS/YE3bL78Peswh9rqUi0Dm5V672EtShENgadn8y 1435497488 From: Sergei Zviagintsev To: Greg Kroah-Hartman , Daniel Mack , David Herrmann , Djalal Harouni Cc: linux-kernel@vger.kernel.org, Sergei Zviagintsev Subject: [PATCH RFC 5/5] kdbus: improve tests on incrementing quota Date: Sun, 28 Jun 2015 16:17:34 +0300 Message-Id: <1435497454-10464-6-git-send-email-sergei@s15v.net> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1435497454-10464-1-git-send-email-sergei@s15v.net> References: <1435497454-10464-1-git-send-email-sergei@s15v.net> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2970 Lines: 88 1) Rewrite quota->memory + memory > U32_MAX as U32_MAX - quota->memory < memory and provide the comment on why we need that check. We have no overflow issue in the original expression when size_t is 32-bit because the previous one (available - quota->memory < memory) guarantees that quota->memory + memory doesn't exceed `available' which is <= U32_MAX in that case. But lets stay explicit rather than implicit, it would save us from describing HOW the code works. 2) Add WARN_ON when quota->msgs > KDBUS_CONN_MAX_MSGS This is somewhat inconsistent, so we need to properly report it. 3) Replace quota->fds + fds < quota->fds || quota->fds + fds > KDBUS_CONN_MAX_FDS_PER_USER with KDBUS_CONN_MAX_FDS_PER_USER - quota->fds < fds and add explicit WARN_ON in the case quota->fds > KDBUS_CONN_MAX_FDS_PER_USER. Reading the code one can assume that the first expression is there to ensure that we won't have an overflow in quota->fds after quota->fds += fds, but what it really does is testing for size_t overflow in `quota->fds + fds' to be safe in the second expression (as fds is size_t, quota->fds is converted to bigger type). Rewrite it in more obvious way. KDBUS_CONN_MAX_FDS_PER_USER is checked at compile time to fill in quota->fds type (there is BUILD_BUG_ON), so no further checks for quota->fds overflow are needed. Signed-off-by: Sergei Zviagintsev --- ipc/kdbus/connection.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ipc/kdbus/connection.c b/ipc/kdbus/connection.c index 12e32de310f5..6556a0f9d44c 100644 --- a/ipc/kdbus/connection.c +++ b/ipc/kdbus/connection.c @@ -701,13 +701,21 @@ int kdbus_conn_quota_inc(struct kdbus_conn *c, struct kdbus_user *u, available = (available - accounted + quota->memory) / 3; if (available < quota->memory || - available - quota->memory < memory || - quota->memory + memory > U32_MAX) + available - quota->memory < memory) return -ENOBUFS; - if (quota->msgs >= KDBUS_CONN_MAX_MSGS) + + /* + * available is size_t and thus it could be greater than U32_MAX. + * Ensure that quota->memory won't overflow. + */ + if (U32_MAX - quota->memory < memory) + return -ENOBUFS; + + if (WARN_ON(quota->msgs > KDBUS_CONN_MAX_MSGS) || + quota->msgs == KDBUS_CONN_MAX_MSGS) return -ENOBUFS; - if (quota->fds + fds < quota->fds || - quota->fds + fds > KDBUS_CONN_MAX_FDS_PER_USER) + if (WARN_ON(quota->fds > KDBUS_CONN_MAX_FDS_PER_USER) || + KDBUS_CONN_MAX_FDS_PER_USER - quota->fds < fds) return -EMFILE; quota->memory += memory; -- 1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/