All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nayna Jain <nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
To: tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Subject: [PATCH v4 2/8] tpm: replace dynamically allocated bios_dir with dentry array
Date: Wed, 28 Sep 2016 04:34:36 -0400	[thread overview]
Message-ID: <1475051682-23060-3-git-send-email-nayna@linux.vnet.ibm.com> (raw)
In-Reply-To: <1475051682-23060-1-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>

bios_dir is defined as struct dentry **bios_dir, which results in
dynamic allocation and therefore possibly a memory leak. This patch
replaces it with struct dentry array(struct dentry *bios_dir[3])
similar to what is done for sysfs groups.

Suggested-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
Signed-off-by: Nayna Jain <nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
Reviewed-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
---
 drivers/char/tpm/tpm-chip.c     |  8 +++---
 drivers/char/tpm/tpm.h          |  3 +-
 drivers/char/tpm/tpm_eventlog.c | 63 +++++++++++++++++++----------------------
 drivers/char/tpm/tpm_eventlog.h | 10 +++----
 4 files changed, 40 insertions(+), 44 deletions(-)

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index e595013..826609d 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -278,14 +278,15 @@ static void tpm_del_char_device(struct tpm_chip *chip)
 
 static int tpm1_chip_register(struct tpm_chip *chip)
 {
+	int rc;
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		return 0;
 
 	tpm_sysfs_add_device(chip);
 
-	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
+	rc = tpm_bios_log_setup(chip);
 
-	return 0;
+	return rc;
 }
 
 static void tpm1_chip_unregister(struct tpm_chip *chip)
@@ -293,8 +294,7 @@ static void tpm1_chip_unregister(struct tpm_chip *chip)
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		return;
 
-	if (chip->bios_dir)
-		tpm_bios_log_teardown(chip->bios_dir);
+	tpm_bios_log_teardown(chip);
 }
 
 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 3e952fb..b5866bb 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -171,7 +171,8 @@ struct tpm_chip {
 	unsigned long duration[3]; /* jiffies */
 	bool duration_adjusted;
 
-	struct dentry **bios_dir;
+	struct dentry *bios_dir[3];
+	unsigned int bios_dir_count;
 
 	const struct attribute_group *groups[3];
 	unsigned int groups_cnt;
diff --git a/drivers/char/tpm/tpm_eventlog.c b/drivers/char/tpm/tpm_eventlog.c
index 75e6644..f1df782 100644
--- a/drivers/char/tpm/tpm_eventlog.c
+++ b/drivers/char/tpm/tpm_eventlog.c
@@ -332,7 +332,8 @@ static int tpm_bios_measurements_open(struct inode *inode,
 	if (!log)
 		return -ENOMEM;
 
-	if ((err = read_log(log)))
+	err = read_log(log);
+	if (err)
 		goto out_free;
 
 	/* now register seq file */
@@ -368,54 +369,48 @@ static int is_bad(void *p)
 	return 0;
 }
 
-struct dentry **tpm_bios_log_setup(const char *name)
+int tpm_bios_log_setup(struct tpm_chip *chip)
 {
-	struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
+	const char *name = dev_name(&chip->dev);
 
-	tpm_dir = securityfs_create_dir(name, NULL);
-	if (is_bad(tpm_dir))
-		goto out;
+	chip->bios_dir_count = 0;
+	chip->bios_dir[chip->bios_dir_count] =
+		securityfs_create_dir(name, NULL);
+	if (is_bad(chip->bios_dir[chip->bios_dir_count]))
+		goto err;
+	chip->bios_dir_count++;
 
-	bin_file =
+	chip->bios_dir[chip->bios_dir_count] =
 	    securityfs_create_file("binary_bios_measurements",
-				   S_IRUSR | S_IRGRP, tpm_dir,
+				   S_IRUSR | S_IRGRP, chip->bios_dir[0],
 				   (void *)&tpm_binary_b_measurments_seqops,
 				   &tpm_bios_measurements_ops);
-	if (is_bad(bin_file))
-		goto out_tpm;
+	if (is_bad(chip->bios_dir[chip->bios_dir_count]))
+		goto err;
+	chip->bios_dir_count++;
 
-	ascii_file =
+	chip->bios_dir[chip->bios_dir_count] =
 	    securityfs_create_file("ascii_bios_measurements",
-				   S_IRUSR | S_IRGRP, tpm_dir,
+				   S_IRUSR | S_IRGRP, chip->bios_dir[0],
 				   (void *)&tpm_ascii_b_measurments_seqops,
 				   &tpm_bios_measurements_ops);
-	if (is_bad(ascii_file))
-		goto out_bin;
+	if (is_bad(chip->bios_dir[chip->bios_dir_count]))
+		goto err;
+	chip->bios_dir_count++;
 
-	ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
-	if (!ret)
-		goto out_ascii;
-
-	ret[0] = ascii_file;
-	ret[1] = bin_file;
-	ret[2] = tpm_dir;
-
-	return ret;
+	return 0;
 
-out_ascii:
-	securityfs_remove(ascii_file);
-out_bin:
-	securityfs_remove(bin_file);
-out_tpm:
-	securityfs_remove(tpm_dir);
-out:
-	return NULL;
+err:
+	tpm_bios_log_teardown(chip);
+	return -EIO;
 }
 
-void tpm_bios_log_teardown(struct dentry **lst)
+void tpm_bios_log_teardown(struct tpm_chip *chip)
 {
 	int i;
 
-	for (i = 0; i < 3; i++)
-		securityfs_remove(lst[i]);
+	for (i = chip->bios_dir_count; i > 0; --i)
+		securityfs_remove(chip->bios_dir[i-1]);
+	chip->bios_dir_count = i;
+
 }
diff --git a/drivers/char/tpm/tpm_eventlog.h b/drivers/char/tpm/tpm_eventlog.h
index 8de62b0..fd3357e 100644
--- a/drivers/char/tpm/tpm_eventlog.h
+++ b/drivers/char/tpm/tpm_eventlog.h
@@ -77,14 +77,14 @@ int read_log(struct tpm_bios_log *log);
 
 #if defined(CONFIG_TCG_IBMVTPM) || defined(CONFIG_TCG_IBMVTPM_MODULE) || \
 	defined(CONFIG_ACPI)
-extern struct dentry **tpm_bios_log_setup(const char *);
-extern void tpm_bios_log_teardown(struct dentry **);
+extern int tpm_bios_log_setup(struct tpm_chip *chip);
+extern void tpm_bios_log_teardown(struct tpm_chip *chip);
 #else
-static inline struct dentry **tpm_bios_log_setup(const char *name)
+static inline int tpm_bios_log_setup(struct tpm_chip *chip)
 {
-	return NULL;
+	return 0;
 }
-static inline void tpm_bios_log_teardown(struct dentry **dir)
+static inline void tpm_bios_log_teardown(struct tpm_chip *chip)
 {
 }
 #endif
-- 
2.5.0


------------------------------------------------------------------------------

  parent reply	other threads:[~2016-09-28  8:34 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-28  8:34 [PATCH v4 0/8] tpm: add the securityfs pseudo files support for TPM 2.0 firmware event log Nayna Jain
     [not found] ` <1475051682-23060-1-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-28  8:34   ` [PATCH v4 1/8] tpm: define a generic open() method for ascii & bios measurements Nayna Jain
2016-09-28  8:34   ` Nayna Jain [this message]
     [not found]     ` <1475051682-23060-3-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-30 18:27       ` [PATCH v4 2/8] tpm: replace dynamically allocated bios_dir with dentry array Jarkko Sakkinen
     [not found]         ` <20160930182703.GA9595-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-09-30 18:30           ` Jason Gunthorpe
     [not found]             ` <20160930183026.GC1867-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-09-30 19:48               ` Jarkko Sakkinen
     [not found]                 ` <20160930194825.GB12710-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-09-30 20:31                   ` Jason Gunthorpe
     [not found]                     ` <20160930203147.GB5722-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-01 12:06                       ` Jarkko Sakkinen
2016-10-01 12:27       ` Jarkko Sakkinen
2016-09-28  8:34   ` [PATCH v4 3/8] tpm: validate event log access before tpm_bios_log_setup Nayna Jain
     [not found]     ` <1475051682-23060-4-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-30 18:57       ` Jarkko Sakkinen
     [not found]         ` <20160930185742.GB9595-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-09-30 19:11           ` Jason Gunthorpe
     [not found]             ` <20160930191112.GA5722-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-09-30 19:45               ` Jarkko Sakkinen
     [not found]                 ` <20160930194538.GA12710-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-01  2:42                   ` Jason Gunthorpe
     [not found]                     ` <20161001024213.GA13028-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-01 11:35                       ` Jarkko Sakkinen
2016-10-01 12:01       ` Jarkko Sakkinen
     [not found]         ` <20161001120125.GC8664-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-01 14:28           ` Jarkko Sakkinen
2016-10-01 16:54           ` Jason Gunthorpe
     [not found]             ` <20161001165436.GB13462-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-01 19:32               ` Jarkko Sakkinen
     [not found]                 ` <20161001193239.GA3862-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-01 23:19                   ` Jarkko Sakkinen
2016-10-02 21:25                   ` Jason Gunthorpe
     [not found]                     ` <20161002212551.GB25872-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-03 12:20                       ` Jarkko Sakkinen
     [not found]                         ` <20161003122013.GA9990-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-03 12:35                           ` Jarkko Sakkinen
     [not found]                             ` <20161003123523.GC9990-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-03 16:35                               ` Jason Gunthorpe
     [not found]                                 ` <20161003163516.GB6801-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-03 20:22                                   ` Jarkko Sakkinen
     [not found]                                     ` <20161003202230.GA14624-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-03 21:11                                       ` Jason Gunthorpe
     [not found]                                         ` <20161003211129.GA26880-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-04  5:26                                           ` Jarkko Sakkinen
     [not found]                                             ` <20161004052651.GB10572-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-04 17:12                                               ` Jason Gunthorpe
     [not found]                                                 ` <20161004171231.GB17149-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-05  8:10                                                   ` Jarkko Sakkinen
2016-10-06 20:11                                                   ` Nayna
     [not found]                                                     ` <57F6AFF1.4000103-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-06 20:17                                                       ` Jason Gunthorpe
2016-10-06 19:58                                   ` Nayna
     [not found]                                     ` <57F6ACF7.6000408-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-06 20:12                                       ` Jason Gunthorpe
2016-10-06 19:56               ` Nayna
     [not found]                 ` <57F6AC7D.9070507-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-06 20:10                   ` Jason Gunthorpe
     [not found]                     ` <20161006201047.GA12085-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-06 20:53                       ` Nayna
2016-10-13 18:51           ` Nayna
     [not found]             ` <57FFD79F.7080405-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-19  2:10               ` Nayna
2016-10-03 17:14       ` Jason Gunthorpe
     [not found]         ` <20161003171419.GE6801-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-09  4:17           ` Nayna
     [not found]             ` <57F9C4C4.2070508-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09 23:25               ` Jason Gunthorpe
     [not found]                 ` <20161009232544.GC24139-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-10  1:53                   ` Nayna
     [not found]                     ` <57FAF49D.7040009-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-10  3:21                       ` Jason Gunthorpe
     [not found]                         ` <20161010032113.GA26363-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-10  4:13                           ` Nayna
     [not found]                             ` <57FB1551.9000806-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-11 16:51                               ` Jason Gunthorpe
     [not found]                                 ` <20161011165143.GA6881-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-11 19:11                                   ` Nayna
     [not found]                                     ` <57FD3949.9050302-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-11 20:15                                       ` Jason Gunthorpe
     [not found]                                         ` <20161011201558.GB21656-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-12  5:16                                           ` Nayna
2016-10-13 18:53                                           ` Nayna
2016-09-28  8:34   ` [PATCH v4 4/8] tpm: redefine read_log() to handle ACPI/OF at runtime Nayna Jain
     [not found]     ` <1475051682-23060-5-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-30 19:05       ` Jarkko Sakkinen
     [not found]         ` <20160930190511.GC9595-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-06 20:38           ` Nayna
     [not found]             ` <57F6B647.1070206-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09 11:29               ` Nayna
     [not found]                 ` <57FA2A0B.7060404-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09 12:05                   ` Jarkko Sakkinen
     [not found]                     ` <20161009120553.GA6224-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-10  3:24                       ` Jason Gunthorpe
2016-09-28  8:34   ` [PATCH v4 5/8] tpm: replace of_find_node_by_name() with dev of_node property Nayna Jain
     [not found]     ` <1475051682-23060-6-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-30 19:12       ` Jarkko Sakkinen
2016-09-28  8:34   ` [PATCH v4 6/8] tpm: remove printk error messages Nayna Jain
     [not found]     ` <1475051682-23060-7-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09  1:55       ` Nayna
     [not found]         ` <57F9A392.7050302-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09 23:22           ` Jason Gunthorpe
     [not found]             ` <20161009232208.GB24139-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2016-10-12 12:55               ` Nayna
2016-09-28  8:34   ` [PATCH v4 7/8] tpm: move event log init functions to tpm_eventlog_init.c Nayna Jain
2016-09-28  8:34   ` [PATCH v4 8/8] tpm: add securityfs support for TPM 2.0 firmware event log Nayna Jain
     [not found]     ` <1475051682-23060-9-git-send-email-nayna-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-09-30 19:24       ` Jarkko Sakkinen
2016-10-01 11:51       ` Jarkko Sakkinen
     [not found]         ` <20161001115154.GB8664-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-09  2:02           ` Nayna
     [not found]             ` <57F9A52C.7050405-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2016-10-09  9:14               ` Jarkko Sakkinen
     [not found]                 ` <20161009091409.GD31891-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2016-10-10 18:54                   ` Nayna
2016-09-28  9:43   ` [PATCH v4 0/8] tpm: add the securityfs pseudo files " Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1475051682-23060-3-git-send-email-nayna@linux.vnet.ibm.com \
    --to=nayna-23vcf4htsmix0ybbhkvfkdbpr1lh4cv8@public.gmane.org \
    --cc=tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.