logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0004-Fix-fread-error-checking.patch (2174B)


  1. From c3c72b271de395d0e4421e736700d60f96614725 Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Sat, 1 May 2021 00:23:39 -0700
  4. Subject: [PATCH] Fix fread error checking
  5. fread returns size_t, an unsigned type, so the error check and
  6. TEMP_FAILURE_RETRY had no effect.
  7. Instead, errors can be detected when fread returns a partial amount
  8. and the ferror flag is set.
  9. ---
  10. libnetlink.c | 27 ++++++++++++++-------------
  11. 1 file changed, 14 insertions(+), 13 deletions(-)
  12. diff --git a/libnetlink.c b/libnetlink.c
  13. index 24948bf..d94bd5f 100644
  14. --- a/libnetlink.c
  15. +++ b/libnetlink.c
  16. @@ -432,7 +432,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
  17. int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
  18. void *jarg)
  19. {
  20. - int status;
  21. + size_t status;
  22. struct sockaddr_nl nladdr;
  23. char buf[8192];
  24. struct nlmsghdr *h = (void*)buf;
  25. @@ -442,18 +442,19 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
  26. nladdr.nl_pid = 0;
  27. nladdr.nl_groups = 0;
  28. - while (1) {
  29. + while (!feof(rtnl)) {
  30. int err, len;
  31. int l;
  32. - status = TEMP_FAILURE_RETRY ( fread(&buf, 1, sizeof(*h), rtnl) );
  33. + status = fread(&buf, 1, sizeof(*h), rtnl);
  34. - if (status < 0) {
  35. - perror("rtnl_from_file: fread");
  36. + if (status < sizeof(*h)) {
  37. + if (ferror(rtnl))
  38. + perror("rtnl_from_file: fread");
  39. + else
  40. + fprintf(stderr, "rtnl_from_file: truncated message\n");
  41. return -1;
  42. }
  43. - if (status == 0)
  44. - return 0;
  45. len = h->nlmsg_len;
  46. l = len - sizeof(*h);
  47. @@ -466,12 +467,11 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
  48. status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
  49. - if (status < 0) {
  50. - perror("rtnl_from_file: fread");
  51. - return -1;
  52. - }
  53. - if (status < l) {
  54. - fprintf(stderr, "rtnl-from_file: truncated message\n");
  55. + if (status < NLMSG_ALIGN(l)) {
  56. + if (ferror(rtnl))
  57. + perror("rtnl_from_file: fread");
  58. + else
  59. + fprintf(stderr, "rtnl_from_file: truncated message\n");
  60. return -1;
  61. }
  62. @@ -479,6 +479,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
  63. if (err < 0)
  64. return err;
  65. }
  66. + return 0;
  67. }
  68. int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
  69. --
  70. 2.37.3