Skip to contentSkip to conversation

tpm1_bios_measurements_next should increase position index

PATCH Superseded Vasily Averin 2 participants 13 messages Jan 23, 2020 – Feb 25, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 tpm1_bios_measurements_next should increase position index

if seq_file .next fuction does not change position index, read after non-zero llseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
 drivers/char/tpm/eventlog/tpm1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/eventlog/tpm1.c +1 -1
index 739b1d9..2c96977 100644
@@ -115,6 +115,7 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	u32 converted_event_size;
 	u32 converted_event_type;
 
+	(*pos)++;
 	converted_event_size = do_endian_conversion(event->event_size);
 
 	v += sizeof(struct tcpa_event) + converted_event_size;
@@ -132,7 +133,6 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	    ((v + sizeof(struct tcpa_event) + converted_event_size) > limit))
 		return NULL;
 
-	(*pos)++;
 	return v;
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines · Vasily Averin

if seq_file .next fuction does not change position index, read after non-zero llseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

* Should be "tpm: tpm1_bios_measurements_next should increase position index"
* Sentences in English start with a capital letter.
* Should probably have Fixes tag.
* Shoud have "Cc: [email protected]" tag.

Same remarks for TPM2 patch.

/Jarkko

#2 lore raw
Quoted 7 lines · Jarkko Sakkinen
Quoted 5 lines · Vasily Averin

if seq_file .next fuction does not change position index, read after non-zero llseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

Quoted 6 lines

* Should be "tpm: tpm1_bios_measurements_next should increase position index"
* Sentences in English start with a capital letter.
* Should probably have Fixes tag.
* Shoud have "Cc: [email protected]" tag.

Same remarks for TPM2 patch.

* Shoud have explanation what kind of output is generated without the fix. "Unexpected output" is does not document the regression.

/Jarkko

#3 lore raw

v2 cover tpm: seq_file .next functions should increase position index

v2: improved patch description

In Aug 2018 NeilBrown noticed
commit 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code and interface") "Some ->next functions do not increment *pos when they return NULL... Note that such ->next functions are buggy and should be fixed.
A simple demonstration is

dd if=/proc/swaps bs=1000 skip=1

Choose any block size larger than the size of /proc/swaps. This will always show the whole last line of /proc/swaps"

Described problem is still actual. If you make lseek into middle of last output line following read will output end of last line and whole last line once again.

$ dd if=/proc/swaps bs=1  # usual output
Filename				Type		Size	Used	Priority
/dev/dm-0                               partition	4194812	97536	-2
104+0 records in
104+0 records out
104 bytes copied
$ dd if=/proc/swaps bs=40 skip=1    # last line was generated twice
dd: /proc/swaps: cannot skip to specified offset
v/dm-0                               partition	4194812	97536	-2
/dev/dm-0                               partition	4194812	97536	-2 
3+1 records in
3+1 records out
131 bytes copied

There are lot of other affected files, I've found 30+ including /proc/net/ip_tables_matches and /proc/sysvipc/*

This patch-set fixes files related to tpm driver:
/sys/kernel/security/tpm0/ascii_bios_measurements /sys/kernel/security/tpm0/binary_bios_measurements

https://bugzilla.kernel.org/show_bug.cgi?id=206283

Vasily Averin (2):
tpm: tpm1_bios_measurements_next should increase position index
tpm: tpm2_bios_measurements_next should increase position index

 drivers/char/tpm/eventlog/tpm1.c | 2 +-
 drivers/char/tpm/eventlog/tpm2.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 1/2 tpm: tpm1_bios_measurements_next should increase position index

In reply to Jarkko Sakkinen

If seq_file .next function does not change position index, read after non-zero lseek can generate unexpected output. In case of /sys/kernel/security/tpm0/ascii_bios_measurements and binary_bios_measurements:
1) read after lseek beyond end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
 drivers/char/tpm/eventlog/tpm1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/eventlog/tpm1.c +1 -1
index 739b1d9..2c96977 100644
@@ -115,6 +115,7 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	u32 converted_event_size;
 	u32 converted_event_type;
 
+	(*pos)++;
 	converted_event_size = do_endian_conversion(event->event_size);
 
 	v += sizeof(struct tcpa_event) + converted_event_size;
@@ -132,7 +133,6 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	    ((v + sizeof(struct tcpa_event) + converted_event_size) > limit))
 		return NULL;
 
-	(*pos)++;
 	return v;
 }
 
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v2 2/2 tpm: tpm2_bios_measurements_next should increase position index

In reply to Jarkko Sakkinen

If seq_file .next function does not change position index, read after non-zero lseek can generate unexpected output. For /sys/kernel/security/tpm0/binary_bios_measurements:
1) read after lseek beyond end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
 drivers/char/tpm/eventlog/tpm2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/eventlog/tpm2.c +1 -1
index b9aeda1..e741b11 100644
@@ -94,6 +94,7 @@ static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
 	size_t event_size;
 	void *marker;
 
+	(*pos)++;
 	event_header = log->bios_event_log;
 
 	if (v == SEQ_START_TOKEN) {
@@ -118,7 +119,6 @@ static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
 	if (((v + event_size) >= limit) || (event_size == 0))
 		return NULL;
 
-	(*pos)++;
 	return v;
 }
 
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [PATCH v2 2/2] tpm: tpm2_bios_measurements_next should increase position index

Quoted 2 lines · Vasily Averin

If seq_file .next function does not change position index, read after non-zero lseek can generate unexpected output.

Is it unwanted or unexpected? Unexpected would be mean random output. I don't think that is the case. Please describe more throughly.

Quoted 8 lines

For /sys/kernel/security/tpm0/binary_bios_measurements:
1) read after lseek beyond end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

No empty line here.

Quoted 1 line

https://bugzilla.kernel.org/show_bug.cgi?id=206283

"Link: https://bugzilla.kernel.org/show_bug.cgi?id=206283"

/Jarkko

#7 lore raw

Re: [PATCH v2 2/2] tpm: tpm2_bios_measurements_next should increase position index

Quoted 7 lines · Jarkko Sakkinen
Quoted 2 lines · Vasily Averin

If seq_file .next function does not change position index, read after non-zero lseek can generate unexpected output.

Is it unwanted or unexpected? Unexpected would be mean random output. I don't think that is the case. Please describe more throughly.

If .next function does not change position index,
following .show function will repeat output related to current position index.

Quoted 16 lines
Quoted 8 lines

For /sys/kernel/security/tpm0/binary_bios_measurements:
1) read after lseek beyond end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

No empty line here.

Quoted 1 line

https://bugzilla.kernel.org/show_bug.cgi?id=206283

"Link: https://bugzilla.kernel.org/show_bug.cgi?id=206283"

/Jarkko

#8 lore raw

Re: [PATCH v2 2/2] tpm: tpm2_bios_measurements_next should increase position index

Quoted 11 lines · Vasily Averin
Quoted 7 lines · Jarkko Sakkinen
Quoted 2 lines · Vasily Averin

If seq_file .next function does not change position index, read after non-zero lseek can generate unexpected output.

Is it unwanted or unexpected? Unexpected would be mean random output. I don't think that is the case. Please describe more throughly.

If .next function does not change position index,
following .show function will repeat output related to current position index.

Thank you. That is clear and concise.

Then, please put that to the commit message instead of "unexpected output".

/Jarkko

#9 lore raw

PATCH v3 1/2 tpm: tpm1_bios_measurements_next should increase position index

If .next function does not change position index, following .show function will repeat output related to current position index.

In case of /sys/kernel/security/tpm0/ascii_bios_measurements and binary_bios_measurements:
1) read after lseek beyound end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")
---
 drivers/char/tpm/eventlog/tpm1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/eventlog/tpm1.c +1 -1
index 739b1d9..2c96977 100644
@@ -115,6 +115,7 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	u32 converted_event_size;
 	u32 converted_event_type;
 
+	(*pos)++;
 	converted_event_size = do_endian_conversion(event->event_size);
 
 	v += sizeof(struct tcpa_event) + converted_event_size;
@@ -132,7 +133,6 @@ static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
 	    ((v + sizeof(struct tcpa_event) + converted_event_size) > limit))
 		return NULL;
 
-	(*pos)++;
 	return v;
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v3 1/2
#10 lore raw

Re: [PATCH v3 1/2] tpm: tpm1_bios_measurements_next should increase position index

Quoted 10 lines · Vasily Averin

If .next function does not change position index, following .show function will repeat output related to current position index.

In case of /sys/kernel/security/tpm0/ascii_bios_measurements and binary_bios_measurements:
1) read after lseek beyound end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Quoted 4 lines
Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

Thank alot!

/Jarkko

#11 lore raw

Re: [PATCH v3 1/2] tpm: tpm1_bios_measurements_next should increase position index

Quoted 11 lines · Jarkko Sakkinen
Quoted 10 lines · Vasily Averin

If .next function does not change position index, following .show function will repeat output related to current position index.

In case of /sys/kernel/security/tpm0/ascii_bios_measurements and binary_bios_measurements:
1) read after lseek beyound end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Quoted 8 lines
Quoted 4 lines
Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")

Thank alot!

Applied both.

I'll try to squeeze these to v5.6.

/Jarkko

#12 lore raw

PATCH v3 2/2 tpm: tpm2_bios_measurements_next should increase position index

In reply to Jarkko Sakkinen

If .next function does not change position index, following .show function will repeat output related to current position index.

For /sys/kernel/security/tpm0/binary_bios_measurements:
1) read after lseek beyound end of file generates whole last line.
2) read after lseek to middle of last line generates expected end of last line and unexpected whole last line once again.

Fixes 1f4aace60b0e ("fs/seq_file.c: simplify seq_file iteration code ...")
---
 drivers/char/tpm/eventlog/tpm2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/eventlog/tpm2.c +1 -1
index b9aeda1..e741b11 100644
@@ -94,6 +94,7 @@ static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
 	size_t event_size;
 	void *marker;
 
+	(*pos)++;
 	event_header = log->bios_event_log;
 
 	if (v == SEQ_START_TOKEN) {
@@ -118,7 +119,6 @@ static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
 	if (((v + event_size) >= limit) || (event_size == 0))
 		return NULL;
 
-	(*pos)++;
 	return v;
 }
 
Signature · 1 line
1.8.3.1