Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp189374pxb; Mon, 16 Aug 2021 03:16:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzrb0yMTEWfDfxSlHojaUDTGZp4O8toY30U+SjLHkTtHiLrl6cErd9iJ7EgLR/ZEaGzYy6 X-Received: by 2002:a50:be81:: with SMTP id b1mr19823970edk.295.1629108984584; Mon, 16 Aug 2021 03:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629108984; cv=none; d=google.com; s=arc-20160816; b=egfUL0kQwH9acN1M/CYDCKyeP8NighwbbUWLoig5xAi1c87VPx5E/VRRTibPqcDi0n wQpRktEk6eQDeD44Gg/gMUZXLyg6jMaJvjBn5wOfDx9hQSPUgpYMulkfyzANlii74L2C u34ffnOwkYubK5H5VOI5Xt7cxVybrdiWHgCXTc7XiPphitwJl/t2c04t+J9E+tnz7H4J qLYkS9UikFmCKxnZVuKdSsDc4Ur+QY41AJGWY31BHVlupKEhd/R5sNVzIEd5VSYW0RCQ vFfsSzA0Er7uQDStpLcJ5fK2PEtgP3zVtog3Db/s+OJu2b2HxOMocNu3/KfeeVWrLath 79iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=7lBav3BiTSHLqodpKTwEx5kosNYnKYUMB4CDNs59htE=; b=mx+iuZxjR3hatb1R/QV2ktP5L+L4G/4RjV6/t1yI1k9g7QD95c5ZKO4vONr9lsq+Bq gtHKVbe3YmBeKYL2NXvQi47EjQEXuAjnGowPHNH15Xb+yFSUy9yCcGHWrRsruX2l3TD8 MjC5C70YA/MzMrP/HD/zPwjXb+aTYEM4ToogQtEazbwm8vPWhNN6PNd6ROQVKml3C1Su 9Agcvn918ulf+5NLLjVpeI20MyECrwsXd+rH2dAbYJTkmGPkXQoy+OVnG+f3Xqoo8IBV wED48U707PHzOs/bXW8rDgoit2/79zce2FDlkVJ5r5q++leiaCF5e/pt5l01MDfpR4JB jXRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=G9o8UuC+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p26si10996077edu.78.2021.08.16.03.16.01; Mon, 16 Aug 2021 03:16:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=G9o8UuC+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230397AbhHPKO7 (ORCPT + 99 others); Mon, 16 Aug 2021 06:14:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229739AbhHPKO7 (ORCPT ); Mon, 16 Aug 2021 06:14:59 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E416AC061764 for ; Mon, 16 Aug 2021 03:14:27 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id n12so19506475plf.4 for ; Mon, 16 Aug 2021 03:14:27 -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=7lBav3BiTSHLqodpKTwEx5kosNYnKYUMB4CDNs59htE=; b=G9o8UuC+GwLrBXxScll6XnRHsD1N3zo8VSdXUWSO9d9GGTZXqJL4YBRJsEY9VB5zAf pMcK2XtoOqUbhESubnhR6EjH98pz0uDEuVfLjWJlgwMqJLISlN121rx9fael6vaEtBsP TBP304OLPlVgUlfY/+UL02BS0UOaZ0h/zrKXnCIfmZgk3yS8jzuZu5xxYEJxEq0diiK3 YNs2wETT+THIUUX8UbRX9EqbHlHnX3Q2VV0k77qrbzHL2k1FswCTwSxprbuGuBlMaQeb mCGnvoGzxTDsArRb0gq+aXRL+LvBy6z5acuCDWbkOSqJKwk9a4CcH4oQJ84qlwfghLse 9Ptw== 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=7lBav3BiTSHLqodpKTwEx5kosNYnKYUMB4CDNs59htE=; b=FzwLN9zWm3biV4/P4PEM3p9FAnnWEpO3UPPwSGgwybvDP7u1Db7Mtqnt2CtomQG26p Ambcm7Rrm7E/w9Uie1K0liUH5XSRwQIKZeB/ALaboI6sR3cNQBPA6/5CJYlqQFZQK4GM Ursonteb/qqG+1YXBCKLp0GHczwlaDr2118s8FBvc+jpcv9rAY8rwIhYptgE4gWaqzHZ cKt++Qe8WmB36wNeWk3WBXLW1EAYy/SGX+boAxh0wwsvEqISle3no2EyHfUnxpk9H132 iUOSiGHL2vRMDjphFZ65x1Lc0qjVGPDcVekNRk7HmufwixQrsYBNPulFNu2vtJDxPlt0 Xdjg== X-Gm-Message-State: AOAM530WOal67qzp5liWkp8tX4+/O+WBWPIEO9BUfCusbDpkERuRCpO4 uX266F8xn9g72i6BGLz1WddzOGDEmBA= X-Received: by 2002:a17:90a:e018:: with SMTP id u24mr16558665pjy.126.1629108867525; Mon, 16 Aug 2021 03:14:27 -0700 (PDT) Received: from localhost ([43.224.245.179]) by smtp.gmail.com with ESMTPSA id 65sm12773508pgi.12.2021.08.16.03.14.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Aug 2021 03:14:26 -0700 (PDT) From: Jiazi Li X-Google-Original-From: Jiazi Li To: Arnd Bergmann , Greg Kroah-Hartman Cc: Jiazi Li , linux-kernel@vger.kernel.org Subject: [PATCH] char: misc: init list head only when needed Date: Mon, 16 Aug 2021 18:12:32 +0800 Message-Id: <853e8529c18c4a71c36b49f9598961fbc3f39682.1628921932.git.lijiazi@xiaomi.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a module successfully registers a misc device. Then, due to some bugs, use same address register misc device again, init list head will corrupt misc_list, resulting in oops when using misc_list. In this scenario, do not init list head, if registration is successful, init list head is also not required. Signed-off-by: Jiazi Li --- drivers/char/misc.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/char/misc.c b/drivers/char/misc.c index ca5141e..2451640 100644 --- a/drivers/char/misc.c +++ b/drivers/char/misc.c @@ -176,7 +176,6 @@ int misc_register(struct miscdevice *misc) int err = 0; bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR); - INIT_LIST_HEAD(&misc->list); mutex_lock(&misc_mtx); @@ -185,7 +184,7 @@ int misc_register(struct miscdevice *misc) if (i >= DYNAMIC_MINORS) { err = -EBUSY; - goto out; + goto err2; } misc->minor = DYNAMIC_MINORS - i - 1; set_bit(i, misc_minors); @@ -195,7 +194,13 @@ int misc_register(struct miscdevice *misc) list_for_each_entry(c, &misc_list, list) { if (c->minor == misc->minor) { err = -EBUSY; - goto out; + /* + * if module use same address double register, + * init list will corrupt misc_list + */ + if (c == misc) + goto err1; + goto err2; } } } @@ -214,7 +219,7 @@ int misc_register(struct miscdevice *misc) misc->minor = MISC_DYNAMIC_MINOR; } err = PTR_ERR(misc->this_device); - goto out; + goto err2; } /* @@ -222,7 +227,11 @@ int misc_register(struct miscdevice *misc) * earlier defaults */ list_add(&misc->list, &misc_list); - out: + mutex_unlock(&misc_mtx); + return 0; + err2: + INIT_LIST_HEAD(&misc->list); + err1: mutex_unlock(&misc_mtx); return err; } -- 2.7.4