i2c: bcm2835: Clear current message and count after a transaction
Subsystems (3)
PATCH 1/1 i2c: bcm2835: Clear current message and count after a transaction
The driver's interrupt handler checks whether a message is currently being handled with the curr_msg pointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, the i2c_start_transfer routine checks for the remaining number of messages to handle in num_msgs.
However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the curr_msg structure, leading to a fatal page fault.
Fix the issue by systematically clearing curr_msg and num_msgs in the driver-wide device structure when a transfer is considered complete.
--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/i2c/busses/i2c-bcm2835.c +3 -0
@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; } + i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ; +if (!i2c_dev->msg_err)return num;
Signature · 1 line
2.20.1
Quoted 10 lines · Paul Kocialkowski
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
Quoted 6 lines
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.
Should not this get a Fixes tag?
Quoted 21 lines
Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +3
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; } + i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ; +if (!i2c_dev->msg_err)return num;
Signature · 1 line
Florian
Hi Paul,
Quoted 30 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 5 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; } + i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;
AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Btw: Why did you leave of the i2c transfer timeout case?
Thanks
Stefan
Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +1
-0+if (!i2c_dev->msg_err)return num;Signature · 1 line
2.20.1
Hi,
Quoted 11 lines · Florian Fainelli
Quoted 10 lines · Paul Kocialkowski
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
Quoted 8 lines
Quoted 6 lines
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Should not this get a Fixes tag?
Yes it totally should! Thanks for the suggestion.
Cheers,
Paul
Quoted 22 lines
Quoted 20 lines
Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +3
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; } + i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ; +if (!i2c_dev->msg_err)return num;
Signature · 3 lines
Paul Kocialkowski, Bootlin (formerly Free Electrons) Embedded Linux and kernel engineering https://bootlin.com
Hi,
Quoted 35 lines · Stefan Wahren
Hi Paul,
Quoted 33 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; }
Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+ i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Do you have a specific case of use-after-free related to these variables in mind that this cleanup would not fix (except for the timeout case)?
The msg_buf element (in association with msg_buf_remaining keeping its previous value) could also lead to similar problems, so I'm thinking of making a bcm2835_i2c_finish_transfer function to group all the cleanups and call that right after wait_for_completion_timeout.
Quoted 1 line
Btw: Why did you leave of the i2c transfer timeout case?
I should definitely have included it, actually.
Cheers,
Paul
Quoted 10 lines
Thanks
StefanQuoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +1
-0+if (!i2c_dev->msg_err)return num;Signature · 1 line
2.20.1
Signature · 3 lines
Paul Kocialkowski, Bootlin (formerly Free Electrons) Embedded Linux and kernel engineering https://bootlin.com
Hi Paul,
Quoted 41 lines
Paul Kocialkowski <[email protected]> hat am 24. Dezember 2018 um 10:10 geschrieben:
Hi,
Quoted 35 lines · Stefan Wahren
Hi Paul,
Quoted 33 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; }
Quoted 8 lines
Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+ i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Do you have a specific case of use-after-free related to these variables in mind that this cleanup would not fix (except for the timeout case)?
okay i was wrong about the use-after-free. But i'm not sure about this scenario on the I2C bus shared with the VC4:
1. ARM core starts I2C transfer (bcm2835_i2c_start_transfer)
2. VC4 triggers a BCM2835_I2C_S_DONE interrupt
3. ARM core catches this interrupt before the VC4
Stefan
Quoted 29 lines
The
msg_bufelement (in association withmsg_buf_remainingkeeping its previous value) could also lead to similar problems, so I'm thinking of making abcm2835_i2c_finish_transferfunction to group all the cleanups and call that right afterwait_for_completion_timeout.Quoted 1 line
Btw: Why did you leave of the i2c transfer timeout case?
I should definitely have included it, actually.
Cheers,
Paul
Quoted 10 lines
Thanks
StefanQuoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +1
-0+if (!i2c_dev->msg_err)return num;Signature · 1 line
2.20.1Signature · 3 lines
Paul Kocialkowski, Bootlin (formerly Free Electrons) Embedded Linux and kernel engineering https://bootlin.com
Hi Stefan,
Quoted 52 lines · Stefan Wahren
Hi Paul,
Quoted 41 lines
Paul Kocialkowski <[email protected]> hat am 24. Dezember 2018 um 10:10 geschrieben:
Hi,
Quoted 35 lines · Stefan Wahren
Hi Paul,
Quoted 33 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; }Quoted 8 lines
Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+ i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Do you have a specific case of use-after-free related to these variables in mind that this cleanup would not fix (except for the timeout case)?
Quoted 5 lines
okay i was wrong about the use-after-free. But i'm not sure about this scenario on the I2C bus shared with the VC4:
1. ARM core starts I2C transfer (
bcm2835_i2c_start_transfer)
2. VC4 triggers aBCM2835_I2C_S_DONEinterrupt
3. ARM core catches this interrupt before the VC4
Well, I thought the VC4 has precedence over the ARM core, so if the interrupts hits in hardware, it should be seen by the VC4 first and forwarded to the ARM core if needed (I might be wrong though). So in that case, perhaps the ARM core wouldn't see the interrupt and just timeout?
Either way, I'm don't think that having both the VC4 and the ARM core poke on the same bus is a scenario we can realisticly aim to support. Although it should definitely not make our driver crash if that happens.
The HDMI DDC bus is an I2C bus shared between the ARM core and VC4, but the VC4 firmware should detect from the device-tree that Linux will attempt to drive the bus and let the ARM core deal with it without interference.
Thanks for your feedback!
Cheers,
Paul
Quoted 28 lines
Quoted 28 lines
The
msg_bufelement (in association withmsg_buf_remainingkeeping its previous value) could also lead to similar problems, so I'm thinking of making abcm2835_i2c_finish_transferfunction to group all the cleanups and call that right afterwait_for_completion_timeout.Quoted 1 line
Btw: Why did you leave of the i2c transfer timeout case?
I should definitely have included it, actually.
Cheers,
Paul
Quoted 10 lines
Thanks
StefanQuoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c +1
-0+if (!i2c_dev->msg_err)return num;Signature · 1 line
2.20.1Signature · 3 lines
Paul Kocialkowski, Bootlin (formerly Free Electrons) Embedded Linux and kernel engineering https://bootlin.com
Signature · 3 lines
Paul Kocialkowski, Bootlin (formerly Free Electrons) Embedded Linux and kernel engineering https://bootlin.com
Quoted 52 lines · Stefan Wahren
Hi Paul,
Quoted 41 lines
Paul Kocialkowski <[email protected]> hat am 24. Dezember 2018 um 10:10 geschrieben:
Hi,
Quoted 35 lines · Stefan Wahren
Hi Paul,
Quoted 33 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; }Quoted 8 lines
Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+ i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Do you have a specific case of use-after-free related to these variables in mind that this cleanup would not fix (except for the timeout case)?
Quoted 5 lines
okay i was wrong about the use-after-free. But i'm not sure about this scenario on the I2C bus shared with the VC4:
1. ARM core starts I2C transfer (
bcm2835_i2c_start_transfer)
2. VC4 triggers aBCM2835_I2C_S_DONEinterrupt
3. ARM core catches this interrupt before the VC4
We don't share I2C buses with VC4, though, right? That would just be totally broken.
Quoted 58 lines
Eric Anholt <[email protected]> hat am 27. Dezember 2018 um 19:15 geschrieben:
Quoted 52 lines · Stefan Wahren
Hi Paul,
Quoted 49 lines
Paul Kocialkowski <[email protected]> hat am 24. Dezember 2018 um 10:10 geschrieben:
Hi,
Quoted 35 lines · Stefan Wahren
Hi Paul,
Quoted 33 lines
Paul Kocialkowski <[email protected]> hat am 21. Dezember 2018 um 13:11 geschrieben:
The driver's interrupt handler checks whether a message is currently being handled with the
curr_msgpointer. When it is NULL, the interrupt is considered to be unexpected. Similarly, thei2c_start_transferroutine checks for the remaining number of messages to handle innum_msgs.However, these values are never cleared and always keep the message and number relevant to the latest transfer (which might be done already and the underlying message memory might have been freed).
When an unexpected interrupt hits with the DONE bit set, the isr will then try to access the flags field of the
curr_msgstructure, leading to a fatal page fault.Fix the issue by systematically clearing
curr_msgandnum_msgsin the driver-wide device structure when a transfer is considered complete.Signed-off-by Paul Kocialkowski <[email protected]>--- drivers/i2c/busses/i2c-bcm2835.c | 3 +++ 1 file changed, 3 insertions(+)drivers/i2c/busses/i2c-bcm2835.c +0
-0@@ -298,6 +298,9 @@ static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],return -ETIMEDOUT ; }Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 302
Quoted 2 lines
drivers/i2c/busses/i2c-bcm2835.c +2
-0+ i2c_dev->curr_msg =NULL ; + i2c_dev->num_msgs =0 ;AFAIU this would reduce the chance of a use-after-free dramatically but not completely.
Do you have a specific case of use-after-free related to these variables in mind that this cleanup would not fix (except for the timeout case)?
Quoted 8 lines
Quoted 5 lines
okay i was wrong about the use-after-free. But i'm not sure about this scenario on the I2C bus shared with the VC4:
1. ARM core starts I2C transfer (
bcm2835_i2c_start_transfer)
2. VC4 triggers aBCM2835_I2C_S_DONEinterrupt
3. ARM core catches this interrupt before the VC4We don't share I2C buses with VC4, though, right? That would just be totally broken.
All i remember is this comment on Github:
https://github.com/anholt/linux/issues/89#issuecomment-280111496
and currently all 3 I2C busses are enabled for the ARM core in bcm2835-rpi.dtsi.
Stefan