iwpm: crash fix for large connections test
[ This isn't really the correct patch to blame. Sorry! -dan ]
Hello Faisal Latif,
The patch dafb5587178a: "iwpm: crash fix for large connections test" from Feb 26, 2016, leads to the following Smatch static checker warning:
drivers/infiniband/core/iwpm_msg.c:437 iwpm_register_pid_cb() warn: 'nlmsg_request' was already freed. drivers/infiniband/core/iwpm_msg.c:509 iwpm_add_mapping_cb() warn: 'nlmsg_request' was already freed. drivers/infiniband/core/iwpm_msg.c:607 iwpm_add_and_query_mapping_cb() warn: 'nlmsg_request' was already freed. drivers/infiniband/core/iwpm_msg.c:806 iwpm_mapping_error_cb() warn: 'nlmsg_request' was already freed.
drivers/infiniband/core/iwpm_msg.c
385 int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
386 {
387 struct iwpm_nlmsg_request *nlmsg_request = NULL;
388 struct nlattr *nltb[IWPM_NLA_RREG_PID_MAX];
389 struct iwpm_dev_data *pm_msg;
390 char *dev_name, *iwpm_name;
391 u32 msg_seq;
392 u8 nl_client;
393 u16 iwpm_version;
394 const char *msg_type = "Register Pid response";
395
396 if (iwpm_parse_nlmsg(cb, IWPM_NLA_RREG_PID_MAX,
397 resp_reg_policy, nltb, msg_type))
398 return -EINVAL;
399
400 msg_seq = nla_get_u32(nltb[IWPM_NLA_RREG_PID_SEQ]);
401 nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
402 if (!nlmsg_request) {
403 pr_info("%s: Could not find a matching request (seq = %u)\n",
404 __func__, msg_seq);
405 return -EINVAL;
406 }
407 pm_msg = nlmsg_request->req_buffer;
408 nl_client = nlmsg_request->nl_client;
409 dev_name = (char *)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]);
410 iwpm_name = (char *)nla_data(nltb[IWPM_NLA_RREG_ULIB_NAME]);
411 iwpm_version = nla_get_u16(nltb[IWPM_NLA_RREG_ULIB_VER]);
412
413 /* check device name, ulib name and version */
414 if (strcmp(pm_msg->dev_name, dev_name) ||
415 strcmp(iwpm_ulib_name, iwpm_name) ||
416 iwpm_version < IWPM_UABI_VERSION_MIN) {
417
418 pr_info("%s: Incorrect info (dev = %s name = %s version = %u)\n",
419 __func__, dev_name, iwpm_name, iwpm_version);
420 nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
421 goto register_pid_response_exit;
422 }
423 iwpm_user_pid = cb->nlh->nlmsg_pid;
424 iwpm_ulib_version = iwpm_version;
425 if (iwpm_ulib_version < IWPM_UABI_VERSION)
426 pr_warn_once("%s: Down level iwpmd/pid %d. Continuing...",
427 __func__, iwpm_user_pid);
428 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
429 pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n",
430 __func__, iwpm_user_pid);
431 iwpm_set_registration(nl_client, IWPM_REG_VALID);
432 register_pid_response_exit:
433 nlmsg_request->request_done = 1;
434 /* always for found nlmsg_request */
435 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);The iwpm_free_nlmsg_request() function will free "nlmsg_request"... It's not clear what the "/* always for found nlmsg_request */" comment means. Maybe it means that the refcount won't drop to zero so the free function won't be called?
436 barrier();
--> 437 up(&nlmsg_request->sem);
^^^^^^^^^^^^^Dereference.
438 return 0;
439 }regards,
dan carpenter
Quoted 66 lines · Dan Carpenter
[ This isn't really the correct patch to blame. Sorry! -dan ]
Hello Faisal Latif,
The patch
dafb5587178a: "iwpm: crash fix for large connections test" from Feb 26, 2016, leads to the following Smatch static checker warning:
drivers/infiniband/core/iwpm_msg.c:437iwpm_register_pid_cb()warn: 'nlmsg_request' was already freed.drivers/infiniband/core/iwpm_msg.c:509iwpm_add_mapping_cb()warn: 'nlmsg_request' was already freed.drivers/infiniband/core/iwpm_msg.c:607iwpm_add_and_query_mapping_cb()warn: 'nlmsg_request' was already freed.drivers/infiniband/core/iwpm_msg.c:806iwpm_mapping_error_cb()warn: 'nlmsg_request' was already freed.
drivers/infiniband/core/iwpm_msg.c385 int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb) 386 { 387 struct iwpm_nlmsg_request *nlmsg_request = NULL; 388 struct nlattr *nltb[IWPM_NLA_RREG_PID_MAX]; 389 struct iwpm_dev_data *pm_msg; 390 char *dev_name, *iwpm_name; 391 u32 msg_seq; 392 u8 nl_client; 393 u16 iwpm_version; 394 const char *msg_type = "Register Pid response"; 395 396 if (iwpm_parse_nlmsg(cb, IWPM_NLA_RREG_PID_MAX, 397 resp_reg_policy, nltb, msg_type)) 398 return -EINVAL; 399 400 msg_seq = nla_get_u32(nltb[IWPM_NLA_RREG_PID_SEQ]); 401 nlmsg_request = iwpm_find_nlmsg_request(msg_seq); 402 if (!nlmsg_request) { 403 pr_info("%s: Could not find a matching request (seq = %u)\n", 404 __func__, msg_seq); 405 return -EINVAL; 406 } 407 pm_msg = nlmsg_request->req_buffer; 408 nl_client = nlmsg_request->nl_client; 409 dev_name = (char *)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]); 410 iwpm_name = (char *)nla_data(nltb[IWPM_NLA_RREG_ULIB_NAME]); 411 iwpm_version = nla_get_u16(nltb[IWPM_NLA_RREG_ULIB_VER]); 412 413 /* check device name, ulib name and version */ 414 if (strcmp(pm_msg->dev_name, dev_name) || 415 strcmp(iwpm_ulib_name, iwpm_name) || 416 iwpm_version < IWPM_UABI_VERSION_MIN) { 417 418 pr_info("%s: Incorrect info (dev = %s name = %s version = %u)\n", 419 __func__, dev_name, iwpm_name, iwpm_version); 420 nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR; 421 goto register_pid_response_exit; 422 } 423 iwpm_user_pid = cb->nlh->nlmsg_pid; 424 iwpm_ulib_version = iwpm_version; 425 if (iwpm_ulib_version < IWPM_UABI_VERSION) 426 pr_warn_once("%s: Down level iwpmd/pid %d. Continuing...", 427 __func__, iwpm_user_pid); 428 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq); 429 pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n", 430 __func__, iwpm_user_pid); 431 iwpm_set_registration(nl_client, IWPM_REG_VALID); 432 register_pid_response_exit: 433 nlmsg_request->request_done = 1; 434 /* always for found nlmsg_request */ 435 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
Quoted 4 lines
The
iwpm_free_nlmsg_request()function will free "nlmsg_request"... It's not clear what the "/* always for foundnlmsg_request*/" comment means. Maybe it means that the refcount won't drop to zero so the free function won't be called?
I think so. The nlmsg_request reference counter is elevated when it is found in iwpm_find_nlmsg_request(). So I assume that it will be at least 2 before call to kref_put(). Most likely, nlmsg_request->sem prevents from parallel threads to decrease that reference counter.
BTW, not IWPM expert.
Thanks
Quoted 86 lines
Subject: Re: [bug report] iwpm: crash fix for large connections test
Quoted 9 lines · Dan Carpenter
[ This isn't really the correct patch to blame. Sorry! -dan ]
Hello Faisal Latif,
The patch
dafb5587178a: "iwpm: crash fix for large connections test" from Feb 26, 2016, leads to the following Smatch static checker warning:
drivers/infiniband/core/iwpm_msg.c:437iwpm_register_pid_cb()warn:'
nlmsg_request' was already freed.Quoted 1 line
drivers/infiniband/core/iwpm_msg.c:509iwpm_add_mapping_cb()warn:'
nlmsg_request' was already freed.Quoted 1 line
drivers/infiniband/core/iwpm_msg.c:607
iwpm_add_and_query_mapping_cb()warn: 'nlmsg_request' was already freed.Quoted 1 line
drivers/infiniband/core/iwpm_msg.c:806iwpm_mapping_error_cb()warn:'
nlmsg_request' was already freed.Quoted 3 lines
drivers/infiniband/core/iwpm_msg.c
385 int iwpm_register_pid_cb(structsk_buff*skb, structnetlink_callback*cb)
Quoted 10 lines
386 { 387 struct iwpm_nlmsg_request *nlmsg_request = NULL; 388 struct nlattr *nltb[IWPM_NLA_RREG_PID_MAX]; 389 struct iwpm_dev_data *pm_msg; 390 char *dev_name, *iwpm_name; 391 u32 msg_seq; 392 u8 nl_client; 393 u16 iwpm_version; 394 const char *msg_type = "Register Pid response"; 395Quoted 8 lines
396 if (iwpm_parse_nlmsg(cb, IWPM_NLA_RREG_PID_MAX, 397 resp_reg_policy, nltb, msg_type)) 398 return -EINVAL; 399 400 msg_seq = nla_get_u32(nltb[IWPM_NLA_RREG_PID_SEQ]); 401 nlmsg_request = iwpm_find_nlmsg_request(msg_seq); 402 if (!nlmsg_request) { 403 pr_info("%s: Could not find a matching request (seq =%u)\n",
Quoted 6 lines
404 __func__, msg_seq); 405 return -EINVAL; 406 } 407 pm_msg = nlmsg_request->req_buffer; 408 nl_client = nlmsg_request->nl_client; 409 dev_name = (char*)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]);
Quoted 1 line
410
iwpm_name= (char*)nla_data(nltb[IWPM_NLA_RREG_ULIB_NAME]);
Quoted 1 line
411
iwpm_version=
nla_get_u16(nltb[IWPM_NLA_RREG_ULIB_VER]);Quoted 7 lines
412 413 /* check device name, ulib name and version */ 414 if (strcmp(pm_msg->dev_name, dev_name) || 415 strcmp(iwpm_ulib_name, iwpm_name) || 416 iwpm_version < IWPM_UABI_VERSION_MIN) { 417 418 pr_info("%s: Incorrect info (dev = %s name = %s version =%u)\n",
Quoted 8 lines
419 __func__, dev_name, iwpm_name, iwpm_version); 420 nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR; 421 goto register_pid_response_exit; 422 } 423 iwpm_user_pid = cb->nlh->nlmsg_pid; 424 iwpm_ulib_version = iwpm_version; 425 if (iwpm_ulib_version < IWPM_UABI_VERSION) 426 pr_warn_once("%s: Down level iwpmd/pid %d.Continuing...",
Quoted 10 lines
427 __func__, iwpm_user_pid); 428 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq); 429 pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n", 430 __func__, iwpm_user_pid); 431 iwpm_set_registration(nl_client, IWPM_REG_VALID); 432 register_pid_response_exit: 433 nlmsg_request->request_done = 1; 434 /* always for found nlmsg_request */ 435 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);Quoted 4 lines
The
iwpm_free_nlmsg_request()function will free "nlmsg_request"... It's not clear what the "/* always for foundnlmsg_request*/" comment means. Maybe it means that the refcount won't drop to zero so the free function won't be called?
Quoted 5 lines
I think so. The
nlmsg_requestreference counter is elevated when it is found iniwpm_find_nlmsg_request(). So I assume that it will be at least 2 before call tokref_put(). Most likely,nlmsg_request->semprevents from parallel threads to decrease that reference counter.
I agree with Leon. The ref count should be 2 here.
However, I don't see why the kref_put() can't be moved after the up(&nlmsg_request->sem) to get rid of the warning.
Regards,
Mustafa
Quoted 10 lines · Mustafa
Quoted 10 lines
Quoted 9 lines
432 register_pid_response_exit: 433 nlmsg_request->request_done = 1; 434 /* always for found nlmsg_request */ 435 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);The
iwpm_free_nlmsg_request()function will free "nlmsg_request"... It's not clear what the "/* always for foundnlmsg_request*/" comment means. Maybe it means that the refcount won't drop to zero so the free function won't be called?
Quoted 9 lines
Quoted 5 lines
I think so. The
nlmsg_requestreference counter is elevated when it is found iniwpm_find_nlmsg_request(). So I assume that it will be at least 2 before call tokref_put(). Most likely,nlmsg_request->semprevents from parallel threads to decrease that reference counter.I agree with Leon. The ref count should be 2 here.
However, I don't see why thekref_put()can't be moved after theup(&nlmsg_request->sem)to get rid of the warning.
Let's not expend too much time trying to silence this warning. One way to silence the warning would be to do:
kref_put(&nlmsg_request->kref, NULL);
I'm conficted about this approach, but no good can come from calling iwpm_free_nlmsg_request() on this path.
A better way to silence the warning would be to do:
drivers/infiniband/core/iwpm_util.c +1 -1
@@ -357,7 +357,7 @@ struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq) inprocess_list) {if (nlmsg_request->nlmsg_seq == echo_seq) { found_request = nlmsg_request; -kref_get (&nlmsg_request->kref); +kref_get (&found_request->kref);break ; } }
But the best way would be to make Smatch parse iwpm_find_nlmsg_request() correctly as-is.
regards,
dan carpenter
So the background here is that Smatch sees this:
kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
and correctly says "if we call iwpm_free_nlmsg_request() then dereferencing nlmsg_request is a use after free". However, the code is holding two references at this point so it will never call iwpm_free_nlmsg_request().
Smatch already checks to see if we are holding two references, but it doesn't parse this code correctly. Smatch could be fixed, but there are other places with similar warnings that are more difficult to fix.
What we could do is create a kref_no_release() function that just calls WARN(). This would silence the warning and, I think, this would make the code more readable.
What do other people think?
regards,
dan carpenter
include/linux/kref.h +5 -0
@@ -45,6 +45,11 @@ static inline void kref_get(struct kref *kref)refcount_inc (&kref->refcount); } +static inline void kref_no_release (struct kref *kref) +{ +WARN (1 ,"Unexpected kref release" ); +} +/** * kref_put - decrement refcount for object. * @kref: object.
drivers/infiniband/core/iwpm_msg.c +4 -4
@@ -432,7 +432,7 @@ int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb) register_pid_response_exit: nlmsg_request->request_done =1 ;/* always for found nlmsg_request */ -kref_put (&nlmsg_request->kref, iwpm_free_nlmsg_request); +kref_put (&nlmsg_request->kref, kref_no_release);barrier ();up (&nlmsg_request->sem);return 0 ;
@@ -504,7 +504,7 @@ int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb) add_mapping_response_exit: nlmsg_request->request_done =1 ;/* always for found request */ -kref_put (&nlmsg_request->kref, iwpm_free_nlmsg_request); +kref_put (&nlmsg_request->kref, kref_no_release);barrier ();up (&nlmsg_request->sem);return 0 ;
@@ -602,7 +602,7 @@ int iwpm_add_and_query_mapping_cb(struct sk_buff *skb, query_mapping_response_exit: nlmsg_request->request_done =1 ;/* always for found request */ -kref_put (&nlmsg_request->kref, iwpm_free_nlmsg_request); +kref_put (&nlmsg_request->kref, kref_no_release);barrier ();up (&nlmsg_request->sem);return 0 ;
@@ -801,7 +801,7 @@ int iwpm_mapping_error_cb(struct sk_buff *skb, struct netlink_callback *cb) nlmsg_request->err_code = err_code; nlmsg_request->request_done =1 ;/* always for found request */ -kref_put (&nlmsg_request->kref, iwpm_free_nlmsg_request); +kref_put (&nlmsg_request->kref, kref_no_release);barrier ();up (&nlmsg_request->sem);return 0 ;
Quoted 13 lines · Dan Carpenter
So the background here is that Smatch sees this:
kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);and correctly says "if we call
iwpm_free_nlmsg_request()then dereferencingnlmsg_requestis a use after free". However, the code is holding two references at this point so it will never calliwpm_free_nlmsg_request().Smatch already checks to see if we are holding two references, but it doesn't parse this code correctly. Smatch could be fixed, but there are other places with similar warnings that are more difficult to fix.
Quoted 5 lines
What we could do is create a
kref_no_release()function that just callsWARN(). This would silence the warning and, I think, this would make the code more readable.What do other people think?
Sure, that looks semi-decent if it helps out with the automated tools.
thanks
greg k-h