Browse Source

Memory protection

master
aluzzardi 13 years ago
parent
commit
c0c11eab3a
8 changed files with 103 additions and 48 deletions
  1. +1
    -0
      Makefile
  2. +5
    -12
      src/conf.c
  3. +2
    -1
      src/device.c
  4. +9
    -8
      src/hal.c
  5. +45
    -0
      src/mem.c
  6. +28
    -0
      src/mem.h
  7. +4
    -3
      src/volume.c
  8. +9
    -24
      src/xpath.c

+ 1
- 0
Makefile View File

@ -10,6 +10,7 @@ LIBS := `pkg-config --libs libxml-2.0` \
# common source files # common source files
SRCS := src/conf.c \ SRCS := src/conf.c \
src/mem.c \
src/log.c \ src/log.c \
src/xpath.c \ src/xpath.c \
src/hal.c \ src/hal.c \


+ 5
- 12
src/conf.c View File

@ -18,6 +18,7 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "mem.h"
#include "conf.h" #include "conf.h"
#include "xpath.h" #include "xpath.h"
#include "log.h" #include "log.h"
@ -71,15 +72,11 @@ static int pusb_conf_parse_options(t_pusb_options *opts,
for (i = 0; opt_list[i].name != NULL; ++i) for (i = 0; opt_list[i].name != NULL; ++i)
{ {
xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1; xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1;
if (!(xpath = malloc(xpath_size)))
{
log_error("malloc error\n");
return (0);
}
xpath = xmalloc(xpath_size);
memset(xpath, 0x00, xpath_size); memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, ""); snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, "");
pusb_conf_options_get_from(opts, xpath, doc); pusb_conf_options_get_from(opts, xpath, doc);
free(xpath);
xfree(xpath);
} }
return (1); return (1);
} }
@ -96,16 +93,12 @@ static int pusb_conf_device_get_property(t_pusb_options *opts,
xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + \ xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + \
strlen(property) + 1; strlen(property) + 1;
if (!(xpath = malloc(xpath_len)))
{
log_error("malloc error!\n");
return (0);
}
xpath = xmalloc(xpath_len);
memset(xpath, 0x00, xpath_len); memset(xpath, 0x00, xpath_len);
snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name, snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name,
property); property);
retval = pusb_xpath_get_string(doc, xpath, store, size); retval = pusb_xpath_get_string(doc, xpath, store, size);
free(xpath);
xfree(xpath);
return (retval); return (retval);
} }


+ 2
- 1
src/device.c View File

@ -19,6 +19,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <dbus/dbus.h> #include <dbus/dbus.h>
#include "mem.h"
#include "conf.h" #include "conf.h"
#include "hal.h" #include "hal.h"
#include "log.h" #include "log.h"
@ -42,7 +43,7 @@ static int pusb_device_connected(t_pusb_options *opts, DBusConnection *dbus)
opts->device.name); opts->device.name);
return (0); return (0);
} }
free(udi);
xfree(udi);
log_info("Device \"%s\" is connected (good).\n", opts->device.name); log_info("Device \"%s\" is connected (good).\n", opts->device.name);
return (1); return (1);
} }


+ 9
- 8
src/hal.c View File

@ -20,6 +20,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include "mem.h"
#include "log.h" #include "log.h"
#include "hal.h" #include "hal.h"
@ -66,8 +67,8 @@ void pusb_hal_free_string_array(char **str_array, int length)
return ; return ;
for (i = 0; i < length; ++i) for (i = 0; i < length; ++i)
free(str_array[i]);
free(str_array);
xfree(str_array[i]);
xfree(str_array);
} }
char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_elements) char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_elements)
@ -76,7 +77,7 @@ char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_eleme
char **buffer; char **buffer;
count = 0; count = 0;
buffer = (char **)malloc(sizeof(char *) * 8);
buffer = (char **)xmalloc(sizeof(char *) * 8);
buffer[0] = NULL; buffer[0] = NULL;
while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING || while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING ||
@ -85,11 +86,11 @@ char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_eleme
const char *value; const char *value;
if ((count % 8) == 0 && count != 0) { if ((count % 8) == 0 && count != 0) {
buffer = realloc(buffer, sizeof (char *) * (count + 8));
buffer = xrealloc(buffer, sizeof (char *) * (count + 8));
} }
dbus_message_iter_get_basic(iter, &value); dbus_message_iter_get_basic(iter, &value);
buffer[count] = strdup(value);
buffer[count] = xstrdup(value);
dbus_message_iter_next(iter); dbus_message_iter_next(iter);
count++; count++;
@ -162,7 +163,7 @@ char *pusb_hal_get_string_property(DBusConnection *dbus,
dbus_message_iter_recurse(&reply_iter, &subiter); dbus_message_iter_recurse(&reply_iter, &subiter);
dbus_message_iter_get_basic(&subiter, &dbus_str); dbus_message_iter_get_basic(&subiter, &dbus_str);
if (dbus_str != NULL) if (dbus_str != NULL)
data = strdup(dbus_str);
data = xstrdup(dbus_str);
dbus_message_unref(reply); dbus_message_unref(reply);
return (data); return (data);
} }
@ -245,7 +246,7 @@ int pusb_hal_check_property(DBusConnection *dbus,
if (!data) if (!data)
return (0); return (0);
retval = (strcmp(data, value) == 0); retval = (strcmp(data, value) == 0);
free(data);
xfree(data);
return (retval); return (retval);
} }
@ -334,7 +335,7 @@ char *pusb_hal_find_item(DBusConnection *dbus,
} }
if (match) if (match)
{ {
udi = strdup(devices[i]);
udi = xstrdup(devices[i]);
break; break;
} }
va_end(ap); va_end(ap);


+ 45
- 0
src/mem.c View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
*
* This file is part of the pam_usb project. pam_usb is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
* Public License version 2, as published by the Free Software Foundation.
*
* pam_usb 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include "mem.h"
void *xmalloc(size_t size)
{
void *data = malloc(size);
assert(data != NULL && "malloc() failed");
return (data);
}
void *xrealloc(void *ptr, size_t size)
{
void *data = realloc(ptr, size);
assert(data != NULL && "realloc() failed");
return (data);
}
void xfree(void *ptr)
{
free(ptr);
}
char *xstrdup(const char *s)
{
char *data = strdup(s);
assert(data != NULL && "strdup() failed");
return (data);
}

+ 28
- 0
src/mem.h View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
*
* This file is part of the pam_usb project. pam_usb is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
* Public License version 2, as published by the Free Software Foundation.
*
* pam_usb 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef PUSB_MEM_H_
# define PUSB_MEM_H_
# include <stdlib.h>
# include <string.h>
void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
void xfree(void *ptr);
char *xstrdup(const char *s);
#endif /* !PUSB_MEM_H_ */

+ 4
- 3
src/volume.c View File

@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mount.h> #include <sys/mount.h>
#include "mem.h"
#include "conf.h" #include "conf.h"
#include "log.h" #include "log.h"
#include "hal.h" #include "hal.h"
@ -79,7 +80,7 @@ static char *pusb_volume_mount_path(t_pusb_options *opts, char *udi, DBusConnect
{ {
log_debug("Device %s is mounted more than once\n", udi); log_debug("Device %s is mounted more than once\n", udi);
} }
char *mount_path = strdup(mount_pathes[0]);
char *mount_path = xstrdup(mount_pathes[0]);
pusb_hal_free_string_array(mount_pathes, n_mount); pusb_hal_free_string_array(mount_pathes, n_mount);
log_debug("Device %s is mounted on %s\n", udi, mount_path); log_debug("Device %s is mounted on %s\n", udi, mount_path);
return (mount_path); return (mount_path);
@ -133,7 +134,7 @@ char *pusb_volume_get(t_pusb_options *opts, DBusConnection *dbus)
} }
if (!pusb_volume_mount(opts, volume_udi, dbus)) if (!pusb_volume_mount(opts, volume_udi, dbus))
{ {
free(volume_udi);
xfree(volume_udi);
return (NULL); return (NULL);
} }
mount_point = pusb_volume_mount_path(opts, volume_udi, dbus); mount_point = pusb_volume_mount_path(opts, volume_udi, dbus);
@ -160,5 +161,5 @@ void pusb_volume_destroy(char *mntpoint)
else else
log_error("Unable to umount %s\n", mntpoint); log_error("Unable to umount %s\n", mntpoint);
} }
free(mntpoint);
xfree(mntpoint);
} }

+ 9
- 24
src/xpath.c View File

@ -18,6 +18,7 @@
#include <libxml/xpath.h> #include <libxml/xpath.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "mem.h"
#include "xpath.h" #include "xpath.h"
#include "log.h" #include "log.h"
@ -126,17 +127,13 @@ int pusb_xpath_get_string_from(xmlDocPtr doc,
int retval; int retval;
xpath_size = strlen(base) + strlen(path) + 1; xpath_size = strlen(base) + strlen(path) + 1;
if (!(xpath = malloc(xpath_size)))
{
log_error("malloc error !\n");
return (0);
}
xpath = xmalloc(xpath_size);
memset(xpath, 0x00, xpath_size); memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path); snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_string(doc, xpath, value, size); retval = pusb_xpath_get_string(doc, xpath, value, size);
if (retval) if (retval)
log_debug("%s%s -> %s\n", base, path, value); log_debug("%s%s -> %s\n", base, path, value);
free(xpath);
xfree(xpath);
return (retval); return (retval);
} }
@ -173,15 +170,11 @@ int pusb_xpath_get_bool_from(xmlDocPtr doc,
int retval; int retval;
xpath_size = strlen(base) + strlen(path) + 1; xpath_size = strlen(base) + strlen(path) + 1;
if (!(xpath = malloc(xpath_size)))
{
log_error("malloc error!\n");
return (0);
}
xpath = xmalloc(xpath_size);
memset(xpath, 0x00, xpath_size); memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path); snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_bool(doc, xpath, value); retval = pusb_xpath_get_bool(doc, xpath, value);
free(xpath);
xfree(xpath);
return (retval); return (retval);
} }
@ -227,15 +220,11 @@ int pusb_xpath_get_time_from(xmlDocPtr doc,
int retval; int retval;
xpath_size = strlen(base) + strlen(path) + 1; xpath_size = strlen(base) + strlen(path) + 1;
if (!(xpath = malloc(xpath_size)))
{
log_error("malloc error!\n");
return (0);
}
xpath = xmalloc(xpath_size);
memset(xpath, 0x00, xpath_size); memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path); snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_time(doc, xpath, value); retval = pusb_xpath_get_time(doc, xpath, value);
free(xpath);
xfree(xpath);
return (retval); return (retval);
} }
@ -259,14 +248,10 @@ int pusb_xpath_get_int_from(xmlDocPtr doc,
int retval; int retval;
xpath_size = strlen(base) + strlen(path) + 1; xpath_size = strlen(base) + strlen(path) + 1;
if (!(xpath = malloc(xpath_size)))
{
log_error("malloc error!\n");
return (0);
}
xpath = xmalloc(xpath_size);
memset(xpath, 0x00, xpath_size); memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path); snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_int(doc, xpath, value); retval = pusb_xpath_get_int(doc, xpath, value);
free(xpath);
xfree(xpath);
return (retval); return (retval);
} }

Loading…
Cancel
Save