Skip to contentSkip to conversation

staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc.

lustre-devel Dan Carpenter 3 participants 9 messages Jan 15, 2018 – Jan 23, 2018

lore
Share & tools
lore
#0 lore raw

[ This code was already buggy, it's just that Neil's change made it
show up in static analysis. - dan ]

Hello NeilBrown,

The patch 3c88bdbbf919: "staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following static checker warning:

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323 kiblnd_connd()
error: dereferencing freed memory 'conn'

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3303 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3304 struct kib_peer *peer = NULL;
3305
3306 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3307 struct kib_conn, ibc_list);
3308 list_del(&conn->ibc_list);
3309 if (conn->ibc_reconnect) {
3310 peer = conn->ibc_peer;
3311 kiblnd_peer_addref(peer);
3312 }
3313
3314 spin_unlock_irqrestore(lock, flags);
3315 dropped_lock = 1;
3316

  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^

Freed

3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^

Use after free

3324 if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3326 &kiblnd_data.kib_reconn_list);
3327 else
3328 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3329 &kiblnd_data.kib_reconn_wait);
3330 }

regards,
dan carpenter

#1 lore raw

Hello Dan,

The function kiblnd_destroy_conn() is conditionally free the conn pointer.

void kiblnd_destroy_conn(kib_conn_t *conn, bool free_conn) {
[?]

                if (free_conn)
                               LIBCFS_FREE(conn, sizeof(*conn));
}

Therefore

Quoted 2 lines
  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^ Freed

Conditionally free if !peer.

Quoted 4 lines
  3318
  3319                          spin_lock_irqsave(lock, flags);
  3320                          if (!peer)
  3321                                  continue;
                                         ^^^^^^^^^^

Check for freed or not. If freed go to next loop.

Quoted 3 lines
  3322
  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^ Use after free

This access is legal because of It was not freed.

Dmitry.

Quoted 60 lines

-----Original Message-----
From: lustre-devel [mailto:lustre-devel-bounces at lists.lustre.org] On Behalf Of Dan Carpenter
Sent: Monday, January 15, 2018 12:27 PM
To: neilb at suse.com
Cc: devel at driverdev.osuosl.org; lustre-devel at lists.lustre.org
Subject: [lustre-devel] [bug report] staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc.

[ This code was already buggy, it's just that Neil's change made it
show up in static analysis. - dan ]

Hello NeilBrown,

The patch 3c88bdbbf919: "staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following static checker warning:

	drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323

kiblnd_connd()
error: dereferencing freed memory 'conn'

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3303 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3304 struct kib_peer *peer = NULL;
3305
3306 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3307 struct kib_conn, ibc_list);
3308 list_del(&conn->ibc_list);
3309 if (conn->ibc_reconnect) {
3310 peer = conn->ibc_peer;
3311 kiblnd_peer_addref(peer);
3312 }
3313
3314 spin_unlock_irqrestore(lock, flags);
3315 dropped_lock = 1;
3316

  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^ Freed
  3318
  3319                          spin_lock_irqsave(lock, flags);
  3320                          if (!peer)
  3321                                  continue;
  3322
  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^ Use after free

3324 if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3326 &kiblnd_data.kib_reconn_list);
3327 else
3328 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3329 &kiblnd_data.kib_reconn_wait);
3330 }

Quoted 6 lines

regards,
dan carpenter
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org

--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park, 17 Krylatskaya Str., Bldg 4, Moscow 121614, Russian Federation

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

#2 lore raw

Hello Dan,

It looks this condition is missed in the code. Sorry we should fix this.

Dmitry.

Quoted 29 lines

-----Original Message-----
From: Eremin, Dmitry
Sent: Monday, January 15, 2018 9:27 PM
To: Dan Carpenter <[email protected]>; neilb at suse.com
Cc: devel at driverdev.osuosl.org; lustre-devel at lists.lustre.org
Subject: RE: [lustre-devel] [bug report] staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc.

Hello Dan,

The function kiblnd_destroy_conn() is conditionally free the conn pointer.

void kiblnd_destroy_conn(kib_conn_t *conn, bool free_conn) { [?]

                if (free_conn)
                               LIBCFS_FREE(conn, sizeof(*conn)); }

Therefore

Quoted 2 lines
  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^ Freed

Conditionally free if !peer.

Quoted 4 lines
  3318
  3319                          spin_lock_irqsave(lock, flags);
  3320                          if (!peer)
  3321                                  continue;

^^^^^^^^^^ Check for freed or not. If freed go to next
loop.

Quoted 73 lines
Quoted 3 lines
  3322
  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^ Use after free

This access is legal because of It was not freed.

Dmitry.

Quoted 66 lines

-----Original Message-----
From: lustre-devel [mailto:lustre-devel-bounces at lists.lustre.org] On Behalf Of Dan Carpenter
Sent: Monday, January 15, 2018 12:27 PM
To: neilb at suse.com
Cc: devel at driverdev.osuosl.org; lustre-devel at lists.lustre.org
Subject: [lustre-devel] [bug report] staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc.

[ This code was already buggy, it's just that Neil's change made it
show up in static analysis. - dan ]

Hello NeilBrown,

The patch 3c88bdbbf919: "staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following static checker warning:

	drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323

kiblnd_connd()
error: dereferencing freed memory 'conn'

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3303 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3304 struct kib_peer *peer = NULL;
3305
3306 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3307 struct kib_conn, ibc_list);
3308 list_del(&conn->ibc_list);
3309 if (conn->ibc_reconnect) {
3310 peer = conn->ibc_peer;
3311 kiblnd_peer_addref(peer);
3312 }
3313
3314 spin_unlock_irqrestore(lock, flags);
3315 dropped_lock = 1;
3316

  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^ Freed
  3318
  3319                          spin_lock_irqsave(lock, flags);
  3320                          if (!peer)
  3321                                  continue;
  3322
  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^ Use after free

3324 if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3326 &kiblnd_data.kib_reconn_list);
3327 else
3328 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3329 &kiblnd_data.kib_reconn_wait);
3330 }

regards,
dan carpenter
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org

--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park, 17 Krylatskaya Str., Bldg 4, Moscow 121614, Russian Federation

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

#3 lore raw
In reply to Eremin
Quoted 9 lines · Dmitry

Hello Dan,

The function kiblnd_destroy_conn() is conditionally free the conn pointer.

void kiblnd_destroy_conn(kib_conn_t *conn, bool free_conn) {
[?]

                if (free_conn)
                               LIBCFS_FREE(conn, sizeof(*conn));

This check is not there in the kernel.org kernel. In the upstream kernel it frees it unconditionally. The "free_conn" parameter was added in commit 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd") but that line must have been left out.

regards,
dan carpenter

#4 lore raw
In reply to Dan Carpenter
Quoted 2 lines · Dan Carpenter

[ This code was already buggy, it's just that Neil's change made it
show up in static analysis. - dan ]

Thanks!

This bug was introduced by

Commit: 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")

which added a "free_conn" arg to kiblnd_destroy_conn(), but never used the arg. Presumably it is meant to say "Don't free something", but exactly what should be free and what shouldn't isn't immediately clear.

Liang: do you remember what you intended for that arg to do?

Thanks,
NeilBrown

Quoted 45 lines

Hello NeilBrown,

The patch 3c88bdbbf919: "staging: lustre: replace simple cases of LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following static checker warning:

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323 kiblnd_connd()
error: dereferencing freed memory 'conn'

drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
3303 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3304 struct kib_peer *peer = NULL;
3305
3306 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3307 struct kib_conn, ibc_list);
3308 list_del(&conn->ibc_list);
3309 if (conn->ibc_reconnect) {
3310 peer = conn->ibc_peer;
3311 kiblnd_peer_addref(peer);
3312 }
3313
3314 spin_unlock_irqrestore(lock, flags);
3315 dropped_lock = 1;
3316

  3317                          kiblnd_destroy_conn(conn, !peer);
                                                    ^^^^

Freed

3318
3319 spin_lock_irqsave(lock, flags);
3320 if (!peer)
3321 continue;
3322

  3323                          conn->ibc_peer = peer;
                                ^^^^^^^^^^^^^^

Use after free

3324 if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3325 list_add_tail(&conn->ibc_list,

                                                       ^^^^^^^^^^^^^^

3326 &kiblnd_data.kib_reconn_list);
3327 else
3328 list_add_tail(&conn->ibc_list,

Quoted 7 lines
                                                       ^^^^^^^^^^^^^^

3329 &kiblnd_data.kib_reconn_wait);
3330 }

regards,
dan carpenter

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180123/af35aec9/attachment.sig>

#5 lore raw

Hi there, probably the function name is misleading, but

L3317: kiblnd_destroy_conn(conn, !peer);

This function will NOT free the connection if peer is NULL, and?

L3320: if (!peer)
L3321 continue;

It means the connection is not freed in L3322 and later.

Regards
Liang

On 23/01/2018, 2:55 PM, "NeilBrown" <[email protected]> wrote:

    On Mon, Jan 15 2018, Dan Carpenter wrote:
    
    > [  This code was already buggy, it's just that Neil's change made it
    >    show up in static analysis.  - dan ]
    
    Thanks!
    
    This bug was introduced by
    
     Commit: 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
    
    which added a "free_conn" arg to kiblnd_destroy_conn(), but never used
    the arg.  Presumably it is meant to say "Don't free something", but
    exactly what should be free and what shouldn't isn't immediately clear.
    
    Liang:  do you remember what you intended for that arg to do?
    
    Thanks,
    NeilBrown
    
    >
    > Hello NeilBrown,
    >
    > The patch 3c88bdbbf919: "staging: lustre: replace simple cases of
    > LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following
    > static checker warning:
    >
    > 	drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323 kiblnd_connd()
    > 	error: dereferencing freed memory 'conn'
    >
    > drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
    >   3303                  if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
    >   3304                          struct kib_peer *peer = NULL;
    >   3305  
    >   3306                          conn = list_entry(kiblnd_data.kib_connd_zombies.next,
    >   3307                                            struct kib_conn, ibc_list);
    >   3308                          list_del(&conn->ibc_list);
    >   3309                          if (conn->ibc_reconnect) {
    >   3310                                  peer = conn->ibc_peer;
    >   3311                                  kiblnd_peer_addref(peer);
    >   3312                          }
    >   3313  
    >   3314                          spin_unlock_irqrestore(lock, flags);
    >   3315                          dropped_lock = 1;
    >   3316  
    >   3317                          kiblnd_destroy_conn(conn, !peer);
    >                                                     ^^^^
    > Freed
    >
    >   3318  
    >   3319                          spin_lock_irqsave(lock, flags);
    >   3320                          if (!peer)
    >   3321                                  continue;
    >   3322  
    >   3323                          conn->ibc_peer = peer;
    >                                 ^^^^^^^^^^^^^^
    > Use after free
    >
    >   3324                          if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
    >   3325                                  list_add_tail(&conn->ibc_list,
    >                                                        ^^^^^^^^^^^^^^
    >
    >   3326                                                &kiblnd_data.kib_reconn_list);
    >   3327                          else
    >   3328                                  list_add_tail(&conn->ibc_list,
    >                                                        ^^^^^^^^^^^^^^
    >
    >   3329                                                &kiblnd_data.kib_reconn_wait);
    >   3330                  }
    >
    > regards,
    > dan carpenter
#6 lore raw

Hi, I just realized the ?free_conn? parameter is unused in kernel source, but it?s actually used in the original patch: https://review.whamcloud.com/#/c/17892 , so I think it should be fixed in the kernel.

Regards
Liang

On 23/01/2018, 4:02 PM, "Zhen, Liang" <[email protected]> wrote:

    Hi there, probably the function name is misleading, but 
    
    L3317: kiblnd_destroy_conn(conn, !peer);
    
    This function will NOT free the connection if peer is NULL, and?
    
    L3320:    if (!peer)
    L3321                  continue;
    
    It means the connection is not freed in L3322 and later.
    
    Regards
    Liang
    
    On 23/01/2018, 2:55 PM, "NeilBrown" <[email protected]> wrote:
    
        On Mon, Jan 15 2018, Dan Carpenter wrote:
        
        > [  This code was already buggy, it's just that Neil's change made it
        >    show up in static analysis.  - dan ]
        
        Thanks!
        
        This bug was introduced by
        
         Commit: 4d99b2581eff ("staging: lustre: avoid intensive reconnecting for ko2iblnd")
        
        which added a "free_conn" arg to kiblnd_destroy_conn(), but never used
        the arg.  Presumably it is meant to say "Don't free something", but
        exactly what should be free and what shouldn't isn't immediately clear.
        
        Liang:  do you remember what you intended for that arg to do?
        
        Thanks,
        NeilBrown
        
        >
        > Hello NeilBrown,
        >
        > The patch 3c88bdbbf919: "staging: lustre: replace simple cases of
        > LIBCFS_ALLOC with kzalloc." from Jan 9, 2018, leads to the following
        > static checker warning:
        >
        > 	drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c:3323 kiblnd_connd()
        > 	error: dereferencing freed memory 'conn'
        >
        > drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd_cb.c
        >   3303                  if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
        >   3304                          struct kib_peer *peer = NULL;
        >   3305  
        >   3306                          conn = list_entry(kiblnd_data.kib_connd_zombies.next,
        >   3307                                            struct kib_conn, ibc_list);
        >   3308                          list_del(&conn->ibc_list);
        >   3309                          if (conn->ibc_reconnect) {
        >   3310                                  peer = conn->ibc_peer;
        >   3311                                  kiblnd_peer_addref(peer);
        >   3312                          }
        >   3313  
        >   3314                          spin_unlock_irqrestore(lock, flags);
        >   3315                          dropped_lock = 1;
        >   3316  
        >   3317                          kiblnd_destroy_conn(conn, !peer);
        >                                                     ^^^^
        > Freed
        >
        >   3318  
        >   3319                          spin_lock_irqsave(lock, flags);
        >   3320                          if (!peer)
        >   3321                                  continue;
        >   3322  
        >   3323                          conn->ibc_peer = peer;
        >                                 ^^^^^^^^^^^^^^
        > Use after free
        >
        >   3324                          if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
        >   3325                                  list_add_tail(&conn->ibc_list,
        >                                                        ^^^^^^^^^^^^^^
        >
        >   3326                                                &kiblnd_data.kib_reconn_list);
        >   3327                          else
        >   3328                                  list_add_tail(&conn->ibc_list,
        >                                                        ^^^^^^^^^^^^^^
        >
        >   3329                                                &kiblnd_data.kib_reconn_wait);
        >   3330                  }
        >
        > regards,
        > dan carpenter
#7 lore raw
Quoted 2 lines · Liang

Hi, I just realized the ?free_conn? parameter is unused in kernel source, but it?s actually used in the original patch: https://review.whamcloud.com/#/c/17892 , so I think it should be fixed in the kernel.

Dmitry already sent a patch for this.

regards,
dan carpenter

#8 lore raw
Quoted 5 lines · Dan Carpenter
Quoted 2 lines · Liang

Hi, I just realized the ?free_conn? parameter is unused in kernel source, but it?s actually used in the original patch: https://review.whamcloud.com/#/c/17892 , so I think it should be fixed in the kernel.

Dmitry already sent a patch for this.

Thanks - I hadn't seen that.
Though it seems Greg isn't too happy with the patch yet... unless I failed to find the update too.

Hopefully it will get sorted soon.

And thanks for finding this bug - it validates the work of converting to more standard interfaces :-)

Thanks,
NeilBrown
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180124/bfe1d8ec/attachment.sig>