Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SECURITY] Prevent rhostname array overflow #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions lwip-2.0.2/src/arch/cc.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/*
* cc.h
*
* Created on: Jul 18, 2017
* Author: haohd
*
* Copyright (C) 2017 miniHome
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_
#define LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_
#endif /* LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_ */
/*
* cc.h
*
* Created on: Jul 18, 2017
* Author: haohd
*
* Copyright (C) 2017 miniHome
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_
#define LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_





#endif /* LWIP_2_0_2_SRC_ARCH_ARCH_CC_H_ */
230 changes: 115 additions & 115 deletions lwip-2.0.2/src/arch/if.c
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
/*
* if.c
*
* Created on: Jul 19, 2017
* Author: haohd
*
* Copyright (C) 2017 miniHome
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
int get_if_address(const char *ifname, uint32_t *ip, uint32_t *mask, uint8_t *mac)
{
char *buffer = NULL;
struct ifreq *ifr;
size_t i;
int ret = -1;
if (ifname == NULL || ip == NULL || mask == NULL || mac == NULL)
{
return -1;
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
return -1;
}
struct ifreq _ifr;
memset(&_ifr, 0, sizeof(_ifr));
_ifr.ifr_addr.sa_family = AF_INET;
strncpy(_ifr.ifr_name, ifname , IFNAMSIZ - 1);
if (0 == ioctl(fd, SIOCGIFHWADDR, &_ifr))
{
memcpy(mac, (unsigned char *)_ifr.ifr_hwaddr.sa_data, 6);
}
buffer = (char *) malloc(2048);
if (buffer == NULL)
{
goto _end;
}
struct ifconf ifc = {
.ifc_len = 2048,
.ifc_buf = (caddr_t) buffer
};
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
{
goto _end;
}
for (i = 0; i < ifc.ifc_len; i += sizeof(*ifr))
{
ifr = (struct ifreq *)(buffer + i);
if (ifr->ifr_addr.sa_family != AF_INET)
{
// only support IPv4
continue;
}
char _ip[16] = {};
struct sockaddr_in * addr = (struct sockaddr_in *) &ifr->ifr_addr;
if (inet_ntop(AF_INET, &addr->sin_addr, _ip, sizeof(_ip)) == NULL)
{
continue;
}
if(!strcmp(ifname, ifr->ifr_name))
{
struct ifreq netmask = {};
strcpy(netmask.ifr_name, ifr->ifr_name);
if (ioctl(fd, SIOCGIFNETMASK, &netmask) != 0)
{
continue;
}
*ip = addr->sin_addr.s_addr;
addr = (struct sockaddr_in *) &netmask.ifr_addr;
*mask = addr->sin_addr.s_addr;
ret = 1;
}
}
_end:
if (buffer)
{
free(buffer);
}
if (fd >= 0)
{
close(fd);
}
return ret;
}
/*
* if.c
*
* Created on: Jul 19, 2017
* Author: haohd
*
* Copyright (C) 2017 miniHome
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>

int get_if_address(const char *ifname, uint32_t *ip, uint32_t *mask, uint8_t *mac)
{
char *buffer = NULL;
struct ifreq *ifr;
size_t i;
int ret = -1;

if (ifname == NULL || ip == NULL || mask == NULL || mac == NULL)
{
return -1;
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
return -1;
}

struct ifreq _ifr;
memset(&_ifr, 0, sizeof(_ifr));
_ifr.ifr_addr.sa_family = AF_INET;
strncpy(_ifr.ifr_name, ifname , IFNAMSIZ - 1);
if (0 == ioctl(fd, SIOCGIFHWADDR, &_ifr))
{
memcpy(mac, (unsigned char *)_ifr.ifr_hwaddr.sa_data, 6);
}

buffer = (char *) malloc(2048);
if (buffer == NULL)
{
goto _end;
}

struct ifconf ifc = {
.ifc_len = 2048,
.ifc_buf = (caddr_t) buffer
};
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
{
goto _end;
}

for (i = 0; i < ifc.ifc_len; i += sizeof(*ifr))
{
ifr = (struct ifreq *)(buffer + i);
if (ifr->ifr_addr.sa_family != AF_INET)
{
// only support IPv4
continue;
}

char _ip[16] = {};
struct sockaddr_in * addr = (struct sockaddr_in *) &ifr->ifr_addr;
if (inet_ntop(AF_INET, &addr->sin_addr, _ip, sizeof(_ip)) == NULL)
{
continue;
}

if(!strcmp(ifname, ifr->ifr_name))
{
struct ifreq netmask = {};
strcpy(netmask.ifr_name, ifr->ifr_name);
if (ioctl(fd, SIOCGIFNETMASK, &netmask) != 0)
{
continue;
}
*ip = addr->sin_addr.s_addr;
addr = (struct sockaddr_in *) &netmask.ifr_addr;
*mask = addr->sin_addr.s_addr;
ret = 1;
}
}
_end:
if (buffer)
{
free(buffer);
}
if (fd >= 0)
{
close(fd);
}
return ret;
}


Loading