Received: from mail-yx1-f48.google.com (mail-yx1-f48.google.com [74.125.224.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38EF03033CC for ; Wed, 24 Jun 2026 19:05:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782327947; cv=none; b=jBHmUCfKiRN5ODbRFSF6qFDWRtglm/UieiYoSmIFQavXbgW/fAk9bewv4lJoIrT1RgeBCqFnc/SMOk3bNq2BsQG+R57ugyQCDl2hisXvIoP+m/41VRke0hw+kykZRRv+uulPsFdJJZ9cAvpbHUMMIr0wzepxAt0Xpavwrmivl9E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782327947; c=relaxed/simple; bh=7o/VL+JzaYNj6MWJjKlOZJXSY8uW6JP6zusBaMZfPRQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=elwLK7q4dJmP5VHGRAXicPNgFUXcKzr2Bpdhr4Of2JNGUvvCd/PWB458j7A/VSw6x+axo2ofckdtpBtr6V878uhfJQBsJQbX7pYZTB+8wzo1J1Qz6aqjjcMji3ZLiOauJ64/J2zFs6bzq8WrxdcVe0li+geWj5z06ohpr0x2snA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hL4souOE; arc=none smtp.client-ip=74.125.224.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hL4souOE" Received: by mail-yx1-f48.google.com with SMTP id 956f58d0204a3-6611689dc10so1941728d50.1 for ; Wed, 24 Jun 2026 12:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782327945; x=1782932745; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gFmjNAXIeEojtZAlteSEq7PdaJeLphASQ1lxGy5x1Us=; b=hL4souOEMkNZ/QSbxcIVJ/1fF561QTE+dTquA2it4o1CKSn8h7RABr+OSjDQqE6NH+ gI/dGv607WLm/OkFND970emcjrdG2fskrDJvqdLCj0BPR2Of9f0zcZSFXCB2ADmpQsAf fJV/3a1ZbpoSa56X4i14+MMEMIXHtmNMMzOGVZNDm17NDaeGp8pA+H4fNHxCwOCh947l nvuS1iJkEah6JznhwfcTpMpHrjtFAmKoe5G3hRD1xeiZ48HuU4158wv1KlN7GMBpLuF9 2JIRYg9m6lJpw8bAsHVTRuaBzuzm9mrSp28deuHvvO5rGAj1LRwLnG6SYC/msWItYxeM jL4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782327945; x=1782932745; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gFmjNAXIeEojtZAlteSEq7PdaJeLphASQ1lxGy5x1Us=; b=B30JfPY6bwYx1CkZNWE2YK+T4HjqPBUXtYFfIUKJZ3zq7z1gQG1dfmTv9DcmMzYWkf xpxVmg5ufNCxLu4H3LTQkg+nIl9c9ut6qKJ189hh7XCkfxLjpLGo8kMbNpIK7ELN/d7k +Vr4vZ9lmXtem8JaD3J9N6Hfbl3PtBOEZ33nnLUC8sMjoxSt7n0ZqUrwjnBoZS404XEE 6ULnasR2uX/7eDu1/6poIbu6D5hRRqtBZ9T3ETzpjuodAXZFs1hJh8WhFA5QhdrFvFxD bwJsHTkIJ52n8cyK6bmjeMISJqcXChu65uBzLQrw33vq+FIUipbmmTY2cNt8wA5viu/7 eDng== X-Gm-Message-State: AOJu0YzLcGj7BZD+kHKsE/ERZUjWs4YmbZgB5ZX7KMb+xwxRD0Roe7a2 5CWoN/vb0qSJ5WvTRdOjsqchxONBB3yrppMlGufhxt+KMv9AOCDpdeAMHU80TK4jv8acAg== X-Gm-Gg: AfdE7cnXj6Z3OQ9brszXjMQcKq9PnAqZoCmolYb4ztvxQHT8E8a260dJf365EKO3of3 Qqsd3qJGxANR1e8hYZNy/d0Z488qxzQzVoiq9M9M5pe6xc1lN3i+XbCeROJhZHM8UbIxdt/+tT7 ZtPQN/i3mOY1ZgGv+pq62Jct3or0ajYZoEWsPXPBVt8850K+kjLecO00mh4YZI539j6Rm4FOcXw c/1IVEkuxmofpbCaYwu6k5YjR28V/6D4yXwJ+PkCNtdRHT/jpR65U8rI8LubyUMQhI65oIqTCii F4VFzmfcOQvuUs0pTLRbZmY92Ci5e2Tb6TyzSIWRJDHcIZztr/WR2kVSYm4Y0k4rraYyiyzA3Cp 3TQC7yTv8yJVOh1w7XqacMPArNaPV/ztjkcb0oLCBS7ayxP3nEVFPHe/l5kmoS07jGA5YKVBFkH oO2TEZ8cLYqEpYiXl+HjmtNpUnIw== X-Received: by 2002:a05:690c:670c:b0:7fd:1970:8206 with SMTP id 00721157ae682-806c0e57bc2mr90503967b3.2.1782327945261; Wed, 24 Jun 2026 12:05:45 -0700 (PDT) Received: from Dev-Null-MSI ([2a0d:3344:52ac:a808:98a4:4381:be45:536f]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8025c877e38sm62097827b3.7.2026.06.24.12.05.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2026 12:05:44 -0700 (PDT) From: Yousef Alhouseen To: Matt Porter , Alexandre Bounine Cc: linux-kernel@vger.kernel.org, Yousef Alhouseen Subject: [PATCH] rapidio: mport_cdev: validate user range arithmetic Date: Wed, 24 Jun 2026 21:04:54 +0200 Message-ID: <20260624190454.2688-1-alhouseenyousef@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Several mport cdev ioctl paths validate user-controlled offset and length pairs with addition. Wrapped sums can pass the bounds checks and later drive config-space accesses, DMA mappings, or mmap offsets outside the intended buffer. Validate ranges with subtraction instead, avoid overflowing the user DMA page count, and use subtraction-based tests when matching buffer handles against registered mappings. Signed-off-by: Yousef Alhouseen --- drivers/rapidio/devices/rio_mport_cdev.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c index 009b3b595..cf5bf6c6d 100644 --- a/drivers/rapidio/devices/rio_mport_cdev.c +++ b/drivers/rapidio/devices/rio_mport_cdev.c @@ -252,7 +252,8 @@ static int rio_mport_maint_rd(struct mport_cdev_priv *priv, void __user *arg, if ((maint_io.offset % 4) || (maint_io.length == 0) || (maint_io.length % 4) || - (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ) + maint_io.offset >= RIO_MAINT_SPACE_SZ || + maint_io.length > RIO_MAINT_SPACE_SZ - maint_io.offset) return -EINVAL; buffer = vmalloc(maint_io.length); @@ -297,7 +298,8 @@ static int rio_mport_maint_wr(struct mport_cdev_priv *priv, void __user *arg, if ((maint_io.offset % 4) || (maint_io.length == 0) || (maint_io.length % 4) || - (maint_io.length + maint_io.offset) > RIO_MAINT_SPACE_SZ) + maint_io.offset >= RIO_MAINT_SPACE_SZ || + maint_io.length > RIO_MAINT_SPACE_SZ - maint_io.offset) return -EINVAL; buffer = vmalloc(maint_io.length); @@ -833,7 +835,15 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode, long pinned; offset = lower_32_bits(offset_in_page(xfer->loc_addr)); + if (xfer->length > ULONG_MAX - offset) { + ret = -EINVAL; + goto err_req; + } nr_pages = PAGE_ALIGN(xfer->length + offset) >> PAGE_SHIFT; + if (nr_pages > INT_MAX) { + ret = -EINVAL; + goto err_req; + } page_list = kmalloc_objs(*page_list, nr_pages); if (page_list == NULL) { @@ -883,7 +893,7 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode, mutex_lock(&md->buf_mutex); list_for_each_entry(map, &md->mappings, node) { if (baddr >= map->phys_addr && - baddr < (map->phys_addr + map->size)) { + baddr - map->phys_addr < map->size) { kref_get(&map->ref); req->map = map; break; @@ -896,7 +906,8 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode, goto err_req; } - if (xfer->length + xfer->offset > req->map->size) { + if (xfer->offset > req->map->size || + xfer->length > req->map->size - xfer->offset) { ret = -EINVAL; goto err_req; } @@ -2194,7 +2205,7 @@ static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma) mutex_lock(&md->buf_mutex); list_for_each_entry(map, &md->mappings, node) { if (baddr >= map->phys_addr && - baddr < (map->phys_addr + map->size)) { + baddr - map->phys_addr < map->size) { found = 1; break; } @@ -2206,7 +2217,7 @@ static int mport_cdev_mmap(struct file *filp, struct vm_area_struct *vma) offset = baddr - map->phys_addr; - if (size + offset > map->size) + if (offset > map->size || size > map->size - offset) return -EINVAL; vma->vm_pgoff = offset >> PAGE_SHIFT; -- 2.54.0