Changeset 344856b8 in rtems


Ignore:
Timestamp:
02/23/15 17:06:21 (9 years ago)
Author:
Jeffrey Hill <johill@…>
Branches:
4.9
Parents:
237595d8
git-author:
Jeffrey Hill <johill@…> (02/23/15 17:06:21)
git-committer:
Gedare Bloom <gedare@…> (02/23/15 17:06:21)
Message:

rpc: misaligned address exception in get_myaddress.c

updates #2249 see #1401

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/librpc/src/rpc/get_myaddress.c

    r237595d8 r344856b8  
    4646#include <sys/socket.h>
    4747#include <stdio.h>
     48#include <stdlib.h>
    4849#include <unistd.h>
    49 #include <sys/mbuf.h>
    5050#include <net/if.h>
    5151#include <sys/ioctl.h>
    5252#include <netinet/in.h>
    5353#include <arpa/inet.h>
     54
     55
     56/*
     57 * Determine the size of an ifreq structure when addresses larger
     58 * than the pifreq structure size may be returned from the kernel.
     59 */
     60static size_t ifreqSize ( struct ifreq *pifreq )
     61{
     62  size_t size = pifreq->ifr_addr.sa_len + sizeof(pifreq->ifr_name);
     63  if ( size < sizeof ( *pifreq ) ) {
     64    size = sizeof ( *pifreq );
     65  }
     66  return size;
     67}
    5468
    5569/*
     
    6478{
    6579        int s;
    66         char buf[BUFSIZ];
    6780        struct ifconf ifc;
    68         struct ifreq ifreq, *ifr, *end;
     81        struct ifreq ifreq, *ifr;
    6982        int loopback = 0, gotit = 0;
    7083
     
    7285                return(-1);
    7386        }
    74         ifc.ifc_len = sizeof (buf);
    75         ifc.ifc_buf = buf;
     87again:
     88        ifc.ifc_len = sizeof ( struct ifreq ) * 8u;
     89        ifc.ifc_buf = malloc ( ifc.ifc_len );
     90  if ( ! ifc.ifc_buf ) {
     91                _RPC_close(s);
     92    return -1;
     93  }
    7694        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
    7795                _RPC_close(s);
     96    free ( ifc.ifc_buf );
    7897                return(-1);
    7998        }
    80 again:
    8199        ifr = ifc.ifc_req;
    82         end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
    83100
    84         while (ifr < end) {
    85                 ifreq = *ifr;
    86                 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
     101        while ( ifc.ifc_len >= ifreqSize ( ifr ) ) {
     102    ifreq = *ifr;
     103                if (ioctl(s, SIOCGIFFLAGS, (char *) &ifreq ) < 0) {
    87104                        _RPC_close(s);
     105      free ( ifc.ifc_buf );
    88106                        return(-1);
    89107                }
     
    99117                        break;
    100118                }
    101                 if (ifr->ifr_addr.sa_len)
    102                         ifr = (struct ifreq *) ((caddr_t) ifr +
    103                               ifr->ifr_addr.sa_len -
    104                               sizeof(struct sockaddr));
    105                 ifr++;
     119
     120    const size_t len = ifreqSize ( ifr );
     121    ifc.ifc_len -= len;
     122    /*
     123     * RTEMS seems to require copy up to properly aligned
     124     * boundary at the beginning of the buffer?
     125     */
     126    memmove ( ifr, len + (char *) ifr, ifc.ifc_len );
    106127        }
    107128        if (gotit == 0 && loopback == 0) {
     129    free ( ifc.ifc_buf );
    108130                loopback = 1;
    109131                goto again;
    110132        }
    111133        (void)_RPC_close(s);
     134  free ( ifc.ifc_buf );
    112135        return (gotit ? 0 : -1);
    113136}
Note: See TracChangeset for help on using the changeset viewer.