Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1811658ybl; Sun, 19 Jan 2020 11:44:56 -0800 (PST) X-Google-Smtp-Source: APXvYqwiOc11bC1Ina//6EFPParExUb5m5obYA6TUOv+m8ihQQhpGjdoNVHIIuRJQyzC0TKPeNK2 X-Received: by 2002:a05:6830:1401:: with SMTP id v1mr13358717otp.360.1579463096051; Sun, 19 Jan 2020 11:44:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579463096; cv=none; d=google.com; s=arc-20160816; b=czi2ro9TEQMpXZt52MwO4BVjhfa+UN6AnVaiDEGP+3AoogXaTPD112m/Q3sp1B6t/n 98zVBvMb/iDo7TboMQVyfKqsG7VJNi46F0rsePhD1aGTcu26j2EVNBYfAwIiicdRr6Rz JItddZ18HvDhijjel/C7uVoWHKVEsk/qATOjusC1+O/zneHvQcWhbvcOA/27q+eCJHnd tGwclySlshAQawSecbv3Tk14475ZPZlvI75a/zbpSBsooV4MAcODlR0jzNVt289yFPSr 3WRg+Q4kdcWi0X1UoF3bEpd4WpAfVTos7iPrN+IfylEtayn+zXd8gaRX+OXgjv8UvmhV qP7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=GYu7/zxMNd86d8ZFJUTXCcQyLaKeKY/49TH/Y+j6Gqg=; b=a+Gcn7yslWGkMKpld85hZV4ylOtc3SyHIM7xGJkEAU3Oqg+0/LIjtZVthoiNXxMgo5 zibvoeiyYDzbXJ7Oqi2ibRalstZDOgeD0on1/AbIu9S4ob5BiVZ6AKMt53niKZwV1/Mc 7sMrAYI2Yc1kOUJ8UEeuJUDRh5qomApQUNA/7XTVExSV3Z0779oR21RW75z0HfiW7R6G tASBrrOb4s4RmUgOqzEEsbZ5jIP8pxHjl6cHMOkN0Qa9fDsZESyY+x7AZy83GyQS3roa er4xv5lxE7KvSO7VoRsp1pv+Yr6k2+UJuuYMuI060xxwXnHhj3xwGjg/Y8yIB97iZ52L vPFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QlOdTQSL; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i140si16340021oib.90.2020.01.19.11.44.19; Sun, 19 Jan 2020 11:44:56 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=QlOdTQSL; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728682AbgASTk6 (ORCPT + 99 others); Sun, 19 Jan 2020 14:40:58 -0500 Received: from mail-pj1-f73.google.com ([209.85.216.73]:37140 "EHLO mail-pj1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727144AbgASTk6 (ORCPT ); Sun, 19 Jan 2020 14:40:58 -0500 Received: by mail-pj1-f73.google.com with SMTP id dw15so9121355pjb.2 for ; Sun, 19 Jan 2020 11:40:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=GYu7/zxMNd86d8ZFJUTXCcQyLaKeKY/49TH/Y+j6Gqg=; b=QlOdTQSL0z+vkc6smWiyAQv9TSrlVmSbT/+AQt+zaKA98cZi+qyrt0DbZ0Xs7MFC20 5aamMs9qMUFIv4nItetzHFmLGTOon1t7k/NHKm/1WDKitaeMEIBoRwXp/gbkCHU/iqrr GQRhGd+/iL+Rj4LFLSMeHGRb6GINCcrUFdWgfK2CJgIZk8C2B8kWld7dCCpbzHjeJRD5 +Z/Hz23oeNBgx79zWK2FGFUjAj4Jue7OPGOw8u7g4AHGoHZxvk+YO2X9LQAbGhqtOK+F Wv4BH83ZnfubP878cLqqML9UPDyyFiXnac8MW1chjhuLxiIgB4Y+BWtjdr798IKXSmGq vppQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=GYu7/zxMNd86d8ZFJUTXCcQyLaKeKY/49TH/Y+j6Gqg=; b=MKH9zc4nY3ofHPyYM2npVRGIuoOsLpvP3RYgm9I+Kn/zf2TgnfDDNsUsbQl3ouUeP/ xM5JCxp5kj4retytKNrIN1/vbbbAG3e73aMn9awYfuYm8uK6k3NUSuIfMXiY04glpKgb cnr4b8LAMJjrwBx20adcZVuAYBgzrAKe73dvlwNG5dDnrBHCft+WxMEMf1sfFGeA7GF+ 7wT7jVuPYDB6M3aIdLDHdmEE0fP+4YQExaWL4hdHa2O2keA+omSboykUoWd08ArS6Z0a DpBL18KWA2O8XKZGpyhVpy6YJ+lnMM4zm4cGzjmR9U8hZZWLSykY2D2zWjJPI/7aItJ7 EV4g== X-Gm-Message-State: APjAAAWBAxY0MgvjFlWYXC4sKHRJ+W5xoJVGrHRJG+MUzZaGfjJx4+8D BM4Es7XQfbh4KDewcNWH6gU+pH/51P6O X-Received: by 2002:a63:7311:: with SMTP id o17mr53720220pgc.29.1579462857433; Sun, 19 Jan 2020 11:40:57 -0800 (PST) Date: Sun, 19 Jan 2020 11:40:40 -0800 Message-Id: <20200119194040.128369-1-brianvv@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH v2 bpf-next] bpf: Fix memory leaks in generic update/delete batch ops From: Brian Vazquez To: Brian Vazquez , Brian Vazquez , Alexei Starovoitov , Daniel Borkmann , "David S . Miller" Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Dan Carpenter , Yonghong Song 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 generic update/delete batch ops functions were using __bpf_copy_key without properly freeing the memory. Handle the memory allocation and copy_from_user separately. Reported-by: Dan Carpenter Fixes: aa2e93b8e58e ("bpf: Add generic support for update and delete batch ops") Signed-off-by: Brian Vazquez Acked-by: Yonghong Song --- v1->v2: - Put Fixes subject in a single line (Yonghong Song) --- kernel/bpf/syscall.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index c26a71460f02f..9a840c57f6df7 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1239,12 +1239,15 @@ int generic_map_delete_batch(struct bpf_map *map, if (!max_count) return 0; + key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); + if (!key) + return -ENOMEM; + for (cp = 0; cp < max_count; cp++) { - key = __bpf_copy_key(keys + cp * map->key_size, map->key_size); - if (IS_ERR(key)) { - err = PTR_ERR(key); + err = -EFAULT; + if (copy_from_user(key, keys + cp * map->key_size, + map->key_size)) break; - } if (bpf_map_is_dev_bound(map)) { err = bpf_map_offload_delete_elem(map, key); @@ -1264,6 +1267,8 @@ int generic_map_delete_batch(struct bpf_map *map, } if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) err = -EFAULT; + + kfree(key); return err; } @@ -1294,18 +1299,21 @@ int generic_map_update_batch(struct bpf_map *map, if (!max_count) return 0; + key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); + if (!key) + return -ENOMEM; + value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); - if (!value) + if (!value) { + kfree(key); return -ENOMEM; + } for (cp = 0; cp < max_count; cp++) { - key = __bpf_copy_key(keys + cp * map->key_size, map->key_size); - if (IS_ERR(key)) { - err = PTR_ERR(key); - break; - } err = -EFAULT; - if (copy_from_user(value, values + cp * value_size, value_size)) + if (copy_from_user(key, keys + cp * map->key_size, + map->key_size) || + copy_from_user(value, values + cp * value_size, value_size)) break; err = bpf_map_update_value(map, f, key, value, -- 2.25.0.341.g760bfbb309-goog