Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp9589041ybl; Fri, 17 Jan 2020 14:57:22 -0800 (PST) X-Google-Smtp-Source: APXvYqwENnLNgFCD2hgHKUc1QJ6vhG53gwwdGtef6PW6bvrS8jTRJGi06jSo1gdHOYek/RMTLABN X-Received: by 2002:a9d:478:: with SMTP id 111mr7747966otc.359.1579301841865; Fri, 17 Jan 2020 14:57:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579301841; cv=none; d=google.com; s=arc-20160816; b=O23qzy6jNjWwslF1Y1EVRulLuY2oyXcytj37+li41wfOyhIRDbzpZ6WTSMYataJyLG jJIQwAdfuiJQStTtqaE5TCCqGlgCQYWc1BQSLinh7Dx42HNXCFQp7IToxWYJUIz/NOfI K2G6tLLc0Vu/rtbSulpoDn7IqUjETqK4hQzKwftksSGS+1HrZxX+1Juz45x99l+KKNVU SqMlS4xqk6VqlHBjyNmeCyh1lNRHbh6ELs2HnlTeydwO6cVfNFtRCOYsHGqC8o1RTzOG ufdvLvduKSsr8pP7izEfdX4Hc7LX4Ib8lbiwV+9txJV1DQft9QrwjlmSzphzYpGJsPOx hCUA== 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=YjsrWHQk2Ef6EHqeIvIXkz/PqYhiYpHlX5C3YmPgP0Q=; b=YK/VtjgV2G4SjWruZB9ULjyL41Wr5kshU5wQ+wywJ2a3eoQRyYdAnPBqGBlMFRxsTt ys5SsMPrySEOkboqhZjva9+xNXy08RFei9x+Z5t2RwUyPmmLun602kkh4uXASmJu3Oh/ 1RWQl4uoCxzFEDZA74ydXW2zxqbtK0hZB2QxVDrz4yBc4wNUy2KYFuKv38mzvmZY0tfm KJQnNPzAb6eqw20IMJWs0M3iGES+j/5O0jOCwf2AhI9ibYPxvJWzoougZRR6ckOIPj6W uf+mLCgAyxhJWlaa0EcEdWoRe8q2g4tOSySPncilCtXXbqNbWexxEEgo0a9zNHH/X2wv Iuqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="mSNZ/egw"; 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 z22si2140583otq.94.2020.01.17.14.57.09; Fri, 17 Jan 2020 14:57:21 -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="mSNZ/egw"; 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 S1729122AbgAQW4P (ORCPT + 99 others); Fri, 17 Jan 2020 17:56:15 -0500 Received: from mail-pg1-f201.google.com ([209.85.215.201]:46039 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729147AbgAQW4P (ORCPT ); Fri, 17 Jan 2020 17:56:15 -0500 Received: by mail-pg1-f201.google.com with SMTP id q1so15247150pge.12 for ; Fri, 17 Jan 2020 14:56:14 -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=YjsrWHQk2Ef6EHqeIvIXkz/PqYhiYpHlX5C3YmPgP0Q=; b=mSNZ/egwZMUmNm1xCRqbVDVxIgJ6PFHFMD97bYM6PSRiI0fnVpa/c12S1s1e8l9lw3 4kJDr5myZh+2kfmLJaaAEkMEyaQ7kZLp+1occpsd9NPK1bruNXoY8zJc7ak14IL03QZe tQYueFop0SCDJZTSzY7KkD4Cg5Snz5DeVEVNli2apaOIfDqXOylKHzuofDWMjNrZJU8N hUtED5YDA2ThS+KjvTVPVSZ9hp2yCUvcNaBB5hVRNC1Y8D5MByJpTWoP+2uK4ggVt1x8 DIfsFSUPEoYc7WqM2Q1f9BmZlW5m8ZrGf39dOPe9VBVxkRcYilUThhxblU6pZgYALu5p SGUQ== 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=YjsrWHQk2Ef6EHqeIvIXkz/PqYhiYpHlX5C3YmPgP0Q=; b=kIFN2tRIon0zT0M/eBO+w9T0VE/S8o7845bxyH5m/0gMAzuZawkY3r4I2vz3N3G6E8 VuIGXgKzpgIOtQ9hHqIdkIE7+Zjq07zDGmKkiI+jxzXJ8QVJyqg1vQeU18RohuWFAqCi oJQSV9gTifA6o2c3ONOJzZgqJwo4quRvwJq6ImZoUy8lUDV2EOoT8eAovfXzNhMoerBv C9eK7aaehdG2WKDuk2q6EB5uzXyQC+assOEEpaur/UHmauFrqUldmCPd5+oTBjtR/3+O +14QuTcfhWyxj6Ts/bOKBQBD0VhApRywuPPVbFHqD80wH59ddqQBfSFqmV6hIWfFDaeu fIjg== X-Gm-Message-State: APjAAAVnEsQZXaAL8wc/sAmyRRmEqkwk/BgSiV243mNPoxnP7ujs2z1r jVSXDob+9Lh2+CqNEFu1+3dDk6uAgdZI X-Received: by 2002:a63:3f82:: with SMTP id m124mr48101369pga.431.1579301774378; Fri, 17 Jan 2020 14:56:14 -0800 (PST) Date: Fri, 17 Jan 2020 14:56:08 -0800 Message-Id: <20200117225608.220838-1-brianvv@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH 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 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 --- 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