mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] isofs: fix undefined behavior in iso_date()
@ 2023-07-09  6:42 Linke Li
  2023-07-09 11:41 ` Matthew Wilcox
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Linke Li @ 2023-07-09  6:42 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Jan Kara, Linke Li

From: Linke Li <lilinke99@gmail.com>

Fix undefined behavior in the code by properly handling the left shift operaion.
Instead of left-shifting a negative value, explicitly cast -1 to an unsigned int
before the shift. This ensures well defined behavior and resolves any potential
issues.

Signed-off-by: Linke Li <lilinke99@gmail.com>
---
 fs/isofs/util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/isofs/util.c b/fs/isofs/util.c
index e88dba721661..4c902901401a 100644
--- a/fs/isofs/util.c
+++ b/fs/isofs/util.c
@@ -37,7 +37,7 @@ int iso_date(u8 *p, int flag)
 
 		/* sign extend */
 		if (tz & 0x80)
-			tz |= (-1 << 8);
+			tz |= ((unsigned int)-1 << 8);
 		
 		/* 
 		 * The timezone offset is unreliable on some disks,
-- 
2.25.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] isofs: fix undefined behavior in iso_date()
  2023-07-09  6:42 [PATCH] isofs: fix undefined behavior in iso_date() Linke Li
@ 2023-07-09 11:41 ` Matthew Wilcox
  2023-07-13  7:22   ` linke li
       [not found] ` <63c6f039-4b33-cdfc-1e49-fc9fc35d513e@web.de>
  2023-07-10  9:56 ` Dan Carpenter
  2 siblings, 1 reply; 7+ messages in thread
From: Matthew Wilcox @ 2023-07-09 11:41 UTC (permalink / raw)
  To: Linke Li; +Cc: linux-fsdevel, linux-kernel, Jan Kara, Linke Li

On Sun, Jul 09, 2023 at 02:42:55PM +0800, Linke Li wrote:
> From: Linke Li <lilinke99@gmail.com>
> 
> Fix undefined behavior in the code by properly handling the left shift operaion.
> Instead of left-shifting a negative value, explicitly cast -1 to an unsigned int
> before the shift. This ensures well defined behavior and resolves any potential
> issues.

This certainly fixes the problem, but wouldn't it be easier to get the
compiler to do the work for us?

#include <stdio.h>

int f(unsigned char *p)
{
	return (signed char)p[0];
}

int main(void)
{
	unsigned char x = 0xa5;

	printf("%d\n", f(&x));

	return 0;
}

prints -91.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] isofs: fix undefined behavior in iso_date()
       [not found] ` <63c6f039-4b33-cdfc-1e49-fc9fc35d513e@web.de>
@ 2023-07-10  6:10   ` linke li
  0 siblings, 0 replies; 7+ messages in thread
From: linke li @ 2023-07-10  6:10 UTC (permalink / raw)
  To: Markus Elfring
  Cc: linux-fsdevel, kernel-janitors, LKML, Jan Kara, Linke Li, Matthew Wilcox

Dear Markus,

Thank you for your valuable feedback, I apologize for my typo in the
description.

> How do you think about to add the tag “Fixes”?

I agree with that.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] isofs: fix undefined behavior in iso_date()
  2023-07-09  6:42 [PATCH] isofs: fix undefined behavior in iso_date() Linke Li
  2023-07-09 11:41 ` Matthew Wilcox
       [not found] ` <63c6f039-4b33-cdfc-1e49-fc9fc35d513e@web.de>
@ 2023-07-10  9:56 ` Dan Carpenter
  2023-07-13 14:11   ` David Laight
  2 siblings, 1 reply; 7+ messages in thread
From: Dan Carpenter @ 2023-07-10  9:56 UTC (permalink / raw)
  To: Linke Li; +Cc: linux-fsdevel, linux-kernel, Jan Kara, Linke Li

It looks like maybe there is an issue with "year" as well.

fs/isofs/util.c
    19  int iso_date(u8 *p, int flag)
    20  {
    21          int year, month, day, hour, minute, second, tz;
    22          int crtime;
    23
    24          year = p[0];
                       ^^^^^
year is 0-255.

    25          month = p[1];
    26          day = p[2];
    27          hour = p[3];
    28          minute = p[4];
    29          second = p[5];
    30          if (flag == 0) tz = p[6]; /* High sierra has no time zone */
    31          else tz = 0;
    32          
    33          if (year < 0) {
                    ^^^^^^^^
But this checks year for < 0 which is impossible.  Should it be:

	year = (signed char)p[0];?

    34                  crtime = 0;
    35          } else {
    36                  crtime = mktime64(year+1900, month, day, hour, minute, second);
    37
    38                  /* sign extend */
    39                  if (tz & 0x80)
    40                          tz |= (-1 << 8);
    41                  
    42                  /*

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] isofs: fix undefined behavior in iso_date()
  2023-07-09 11:41 ` Matthew Wilcox
@ 2023-07-13  7:22   ` linke li
  0 siblings, 0 replies; 7+ messages in thread
From: linke li @ 2023-07-13  7:22 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: Linke Li, linux-fsdevel, linux-kernel, Jan Kara

Thanks for your reply.
> This certainly fixes the problem, but wouldn't it be easier to get the
> compiler to do the work for us?
 I don't know which solution is better, but it does avoid this problem.
Like
    tz = (int)(signed char)p[6];

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH] isofs: fix undefined behavior in iso_date()
  2023-07-10  9:56 ` Dan Carpenter
@ 2023-07-13 14:11   ` David Laight
  2023-07-13 14:26     ` Dan Carpenter
  0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2023-07-13 14:11 UTC (permalink / raw)
  To: 'Dan Carpenter', Linke Li
  Cc: linux-fsdevel, linux-kernel, Jan Kara, Linke Li

From: Dan Carpenter
> Sent: 10 July 2023 10:57
> 
> It looks like maybe there is an issue with "year" as well.
> 
> fs/isofs/util.c
>     19  int iso_date(u8 *p, int flag)
>     20  {
>     21          int year, month, day, hour, minute, second, tz;
>     22          int crtime;
>     23
>     24          year = p[0];
>                        ^^^^^
> year is 0-255.
....
>     32
>     33          if (year < 0) {
>                     ^^^^^^^^
> But this checks year for < 0 which is impossible.  Should it be:
> 
> 	year = (signed char)p[0];?

Or not?

What happens in 2027 ?
I bet the value has to be treated an unsigned.

> 
>     34                  crtime = 0;
>     35          } else {
>     36                  crtime = mktime64(year+1900, month, day, hour, minute, second);
>     37
>     38                  /* sign extend */
>     39                  if (tz & 0x80)
>     40                          tz |= (-1 << 8);

Just change the definition of tz from 'int' to 's8'
and it will all happen 'by magic'.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] isofs: fix undefined behavior in iso_date()
  2023-07-13 14:11   ` David Laight
@ 2023-07-13 14:26     ` Dan Carpenter
  0 siblings, 0 replies; 7+ messages in thread
From: Dan Carpenter @ 2023-07-13 14:26 UTC (permalink / raw)
  To: David Laight; +Cc: Linke Li, linux-fsdevel, linux-kernel, Jan Kara, Linke Li

On Thu, Jul 13, 2023 at 02:11:02PM +0000, David Laight wrote:
> From: Dan Carpenter
> > Sent: 10 July 2023 10:57
> > 
> > It looks like maybe there is an issue with "year" as well.
> > 
> > fs/isofs/util.c
> >     19  int iso_date(u8 *p, int flag)
> >     20  {
> >     21          int year, month, day, hour, minute, second, tz;
> >     22          int crtime;
> >     23
> >     24          year = p[0];
> >                        ^^^^^
> > year is 0-255.
> ....
> >     32
> >     33          if (year < 0) {
> >                     ^^^^^^^^
> > But this checks year for < 0 which is impossible.  Should it be:
> > 
> > 	year = (signed char)p[0];?
> 
> Or not?
> 
> What happens in 2027 ?
> I bet the value has to be treated an unsigned.

Yeah.  Good point.  We could delete that if statement and pull the whole
function in a tab.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-07-13 14:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-09  6:42 [PATCH] isofs: fix undefined behavior in iso_date() Linke Li
2023-07-09 11:41 ` Matthew Wilcox
2023-07-13  7:22   ` linke li
     [not found] ` <63c6f039-4b33-cdfc-1e49-fc9fc35d513e@web.de>
2023-07-10  6:10   ` linke li
2023-07-10  9:56 ` Dan Carpenter
2023-07-13 14:11   ` David Laight
2023-07-13 14:26     ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®