Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1330601ybn; Wed, 25 Sep 2019 16:30:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxkE7uhzxaf/WVKkR7x1R8/a5c+oxceRmB9hWNsXln/ztr6nEFMvWWmigoijparAS4bf8aL X-Received: by 2002:a50:95c1:: with SMTP id x1mr577513eda.180.1569454233274; Wed, 25 Sep 2019 16:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569454233; cv=none; d=google.com; s=arc-20160816; b=XZWZKt2GsFiS8n5k/GbB1hOUdpwetUSS/Em/nXP7zdXdu3HiykQtUu8ncQPmfAaNuB RAO9bv2B2vgAj9xr9gkoiR3JB/fWFiHRDlQVN/zGuyZa/S/vba1+uZ2pV2iDnJd4x0sG jvMKAv8AWbhqqUjgBgJ+tKvVJlXkS7ibVijxPdPbGzsVXrbnnZ8QJRYPZ3daUDzuj7XV pAkBFa68H3HknE1CNui+HQn6p9X4Gk8pNEdSrrsAl0b0Tf7GZx6YO7YBkFkVhu3GQRen 9srMhK3kvJkRNiZMfS6S9FJH13NDJmeCezyu4G5xZu9sfESdbT+J+UFPRJYNaeHUjyuK Q04A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:message-id:date:subject:cc:from :dkim-signature; bh=O4biPIS8QtKs0W4x755+TgKPEkw6EcSzOJulQCgOdjc=; b=RLwHscvGfRojt+QiFeG27k7qZs5C+L4N0EbOhhjHjURXYydvwZdZL1x7WAUh+Y9wTK ENOn7PXShKVW71LaaxkuC8LXfDn1zLRgcxOZiCEqROS3GHo8P1rBUNSAR1/QMvl5Cm48 WqpVQKcgPbCHun5P7OR/7FE+g6l3WLDnQdAdqsmAnmebfMFkGRxAPxfw8CAGhul0Bm9P Bc8IXjVzcKooADBe1EfPARfU9lBTvw7onKGF78heHudFfFhsUEqFLb2MIAq2OkFXnnYG IuW+UJPnEu5m0FAzQ0r8OMPG9nowhQbNuW9stDIHDMVvlTJwkCPqw0wWtoCaWlnp3tqu FGPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=njkIaQHg; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d1si138153ejt.354.2019.09.25.16.30.09; Wed, 25 Sep 2019 16:30:33 -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; dkim=fail header.i=@gmail.com header.s=20161025 header.b=njkIaQHg; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502479AbfIWUKJ (ORCPT + 99 others); Mon, 23 Sep 2019 16:10:09 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:41557 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502454AbfIWUKI (ORCPT ); Mon, 23 Sep 2019 16:10:08 -0400 Received: by mail-io1-f67.google.com with SMTP id r26so36584918ioh.8; Mon, 23 Sep 2019 13:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=O4biPIS8QtKs0W4x755+TgKPEkw6EcSzOJulQCgOdjc=; b=njkIaQHgxTxbxvCQR+vWZUivpNX+akdE2x1/MQJw6UKmijwTx7tDcioLHiCWPOR1As 2ezrnErR4qB8uh5MPY9oEv+uSM+Yzw/FRYBhw8dUYeyi36bRn1esr9cA/moDArP0sFZO LGNq6sY8q+rH4eZD8M9rBBLYCIA63O53qsuJg3l4L71c0iViJsYoVlmixUSG9VxlEIL9 C3EhOLUS41GYEj5FzzL8m5OW+2FjNCBFan5Rp0exvo2AFgkvJbI1M5+zlSAjo9oBDfMA 880tkUs8fWGcC0yy1IzZjl/jqMc0dUMUbOBiRvEH3wl02pg2sacrZ15uiGq8kAeBlwgi 5zHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=O4biPIS8QtKs0W4x755+TgKPEkw6EcSzOJulQCgOdjc=; b=rW2nbo+pDSkLeS5ng6kdDtZM5TuPRXOOBzLAeCX/Y/DAmsiYkvm8npK+lW3KhqGDY0 3m3CKFlK4HMjoSOLq6tsl05SdgZRa5jDgKVu0XmgnpdR1e/PJTYnVIAzVB9YJOmGKVQ0 lqBmH7nKbB30HR6vWSQWtvlcfX25h/7hC3axocJq8j5FxexmiVWJ901S1kl3F+K0Ttj8 3K/G1kqBS2TTrzHgyp/wB1Ij9FCyHBYMWitI7iptjnyrREW3d5/hR6+cHTtt411P3q/A 3yMoK/1pHhq6prgov44rCWM+wRrKtE+UWdg1Jbb7zJKSleHaDErPIkRDPzLAgTwTHzcF Fz+g== X-Gm-Message-State: APjAAAVRYJv86ukZKq8zfgwVZjuTGGgq3QT3h658n3JjpHMwz61TWn/d pjmud3VdMrDfmI6Pf2gnc3c= X-Received: by 2002:a05:6602:2244:: with SMTP id o4mr1238924ioo.107.1569269407973; Mon, 23 Sep 2019 13:10:07 -0700 (PDT) Received: from cs-dulles.cs.umn.edu (cs-dulles.cs.umn.edu. [128.101.35.54]) by smtp.googlemail.com with ESMTPSA id d9sm10877889ioq.9.2019.09.23.13.10.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 13:10:07 -0700 (PDT) From: Navid Emamdoost Cc: emamd001@umn.edu, smccaman@umn.edu, kjlu@umn.edu, Navid Emamdoost , Josef Bacik , Jens Axboe , linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org Subject: [PATCH] nbd: prevent memory leak Date: Mon, 23 Sep 2019 15:09:58 -0500 Message-Id: <20190923200959.29643-1-navid.emamdoost@gmail.com> X-Mailer: git-send-email 2.17.1 To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the reallocted memory is leak. The correct behaviour should be assigning the reallocted memory to config->socks right after success. Signed-off-by: Navid Emamdoost --- drivers/block/nbd.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index a8e3815295fe..8ae3bd2e7b30 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -987,14 +987,15 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, sockfd_put(sock); return -ENOMEM; } + + config->socks = socks; + nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL); if (!nsock) { sockfd_put(sock); return -ENOMEM; } - config->socks = socks; - nsock->fallback_index = -1; nsock->dead = false; mutex_init(&nsock->tx_lock); -- 2.17.1