mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
build: avoid unsafe functions in libgen.h
POSIX says that both basename() and dirname() may return static storage (aka they need not be thread-safe); and that they may but not must modify their input argument. Furthermore, <libgen.h> is not available on all platforms. For these reasons, you should never use these functions in a multi-threaded library. Gnulib instead recommends a way to avoid the portability nightmare: gnulib's "dirname.h" provides useful thread-safe counterparts. The obvious dir_name() and base_name() are GPL (because they malloc(), but call exit() on failure) so we can't use them; but the LGPL variants mdir_name() (malloc's or returns NULL) and last_component (always points into the incoming string without modifying it, differing from basename semantics only on corner cases like the empty string that we shouldn't be hitting in the first place) are already in use in libvirt. This finishes the swap over to the safe functions. * cfg.mk (sc_prohibit_libgen): New rule. * src/util/vircgroup.c: Fix offenders. * src/parallels/parallels_storage.c (parallelsPoolAddByDomain): Likewise. * src/parallels/parallels_network.c (parallelsGetBridgedNetInfo): Likewise. * src/node_device/node_device_udev.c (udevProcessSCSIHost) (udevProcessSCSIDevice): Likewise. * src/storage/storage_backend_disk.c (virStorageBackendDiskDeleteVol): Likewise. * src/util/virpci.c (virPCIGetDeviceAddressFromSysfsLink): Likewise. * src/util/virstoragefile.h (_virStorageFileMetadata): Avoid false positive. Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
09c9395a59
commit
1fbf190554
6
cfg.mk
6
cfg.mk
@ -493,6 +493,12 @@ sc_prohibit_gethostby:
|
|||||||
halt='use getaddrinfo, not gethostby*' \
|
halt='use getaddrinfo, not gethostby*' \
|
||||||
$(_sc_search_regexp)
|
$(_sc_search_regexp)
|
||||||
|
|
||||||
|
# dirname and basename from <libgen.h> are not required to be thread-safe
|
||||||
|
sc_prohibit_libgen:
|
||||||
|
@prohibit='( (base|dir)name *\(|include .libgen\.h)' \
|
||||||
|
halt='use functions from gnulib "dirname.h", not <libgen.h>' \
|
||||||
|
$(_sc_search_regexp)
|
||||||
|
|
||||||
# raw xmlGetProp requires some nasty casts
|
# raw xmlGetProp requires some nasty casts
|
||||||
sc_prohibit_xmlGetProp:
|
sc_prohibit_xmlGetProp:
|
||||||
@prohibit='\<xmlGetProp *\(' \
|
@prohibit='\<xmlGetProp *\(' \
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* node_device_udev.c: node device enumeration - libudev implementation
|
* node_device_udev.c: node device enumeration - libudev implementation
|
||||||
*
|
*
|
||||||
* Copyright (C) 2009-2012 Red Hat, Inc.
|
* Copyright (C) 2009-2013 Red Hat, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@ -26,6 +26,7 @@
|
|||||||
#include <scsi/scsi.h>
|
#include <scsi/scsi.h>
|
||||||
#include <c-ctype.h>
|
#include <c-ctype.h>
|
||||||
|
|
||||||
|
#include "dirname.h"
|
||||||
#include "node_device_udev.h"
|
#include "node_device_udev.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "node_device_conf.h"
|
#include "node_device_conf.h"
|
||||||
@ -653,7 +654,7 @@ static int udevProcessSCSIHost(struct udev_device *device ATTRIBUTE_UNUSED,
|
|||||||
union _virNodeDevCapData *data = &def->caps->data;
|
union _virNodeDevCapData *data = &def->caps->data;
|
||||||
char *filename = NULL;
|
char *filename = NULL;
|
||||||
|
|
||||||
filename = basename(def->sysfs_path);
|
filename = last_component(def->sysfs_path);
|
||||||
|
|
||||||
if (!STRPREFIX(filename, "host")) {
|
if (!STRPREFIX(filename, "host")) {
|
||||||
VIR_ERROR(_("SCSI host found, but its udev name '%s' does "
|
VIR_ERROR(_("SCSI host found, but its udev name '%s' does "
|
||||||
@ -774,7 +775,7 @@ static int udevProcessSCSIDevice(struct udev_device *device ATTRIBUTE_UNUSED,
|
|||||||
union _virNodeDevCapData *data = &def->caps->data;
|
union _virNodeDevCapData *data = &def->caps->data;
|
||||||
char *filename = NULL, *p = NULL;
|
char *filename = NULL, *p = NULL;
|
||||||
|
|
||||||
filename = basename(def->sysfs_path);
|
filename = last_component(def->sysfs_path);
|
||||||
|
|
||||||
if (udevStrToLong_ui(filename, &p, 10, &data->scsi.host) == -1) {
|
if (udevStrToLong_ui(filename, &p, 10, &data->scsi.host) == -1) {
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
* parallels_storage.c: core privconn functions for managing
|
* parallels_storage.c: core privconn functions for managing
|
||||||
* Parallels Cloud Server hosts
|
* Parallels Cloud Server hosts
|
||||||
*
|
*
|
||||||
|
* Copyright (C) 2013 Red Hat, Inc.
|
||||||
* Copyright (C) 2012 Parallels, Inc.
|
* Copyright (C) 2012 Parallels, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
@ -23,6 +24,7 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
|
#include "dirname.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
@ -64,7 +66,7 @@ static int parallelsGetBridgedNetInfo(virNetworkDefPtr def, virJSONValuePtr jobj
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(def->bridge = strdup(basename(bridgePath)))) {
|
if (!(def->bridge = strdup(last_component(bridgePath)))) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
* parallels_storage.c: core driver functions for managing
|
* parallels_storage.c: core driver functions for managing
|
||||||
* Parallels Cloud Server hosts
|
* Parallels Cloud Server hosts
|
||||||
*
|
*
|
||||||
|
* Copyright (C) 2013 Red Hat, Inc.
|
||||||
* Copyright (C) 2012 Parallels, Inc.
|
* Copyright (C) 2012 Parallels, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
@ -28,9 +29,9 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <libgen.h>
|
|
||||||
|
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
|
#include "dirname.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "configmake.h"
|
#include "configmake.h"
|
||||||
#include "virstoragefile.h"
|
#include "virstoragefile.h"
|
||||||
@ -230,13 +231,12 @@ parallelsPoolAddByDomain(virConnectPtr conn, virDomainObjPtr dom)
|
|||||||
virStoragePoolObjPtr pool = NULL;
|
virStoragePoolObjPtr pool = NULL;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
if (!(poolPath = strdup(pdom->home))) {
|
poolPath = mdir_name(pdom->home);
|
||||||
|
if (!poolPath) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
poolPath = dirname(poolPath);
|
|
||||||
|
|
||||||
for (j = 0; j < pools->count; j++) {
|
for (j = 0; j < pools->count; j++) {
|
||||||
if (STREQ(poolPath, pools->objs[j]->def->target.path)) {
|
if (STREQ(poolPath, pools->objs[j]->def->target.path)) {
|
||||||
pool = pools->objs[j];
|
pool = pools->objs[j];
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "dirname.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "storage_backend_disk.h"
|
#include "storage_backend_disk.h"
|
||||||
@ -728,8 +729,8 @@ virStorageBackendDiskDeleteVol(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev_name = basename(devpath);
|
dev_name = last_component(devpath);
|
||||||
srcname = basename(pool->def->source.devices[0].path);
|
srcname = last_component(pool->def->source.devices[0].path);
|
||||||
VIR_DEBUG("dev_name=%s, srcname=%s", dev_name, srcname);
|
VIR_DEBUG("dev_name=%s, srcname=%s", dev_name, srcname);
|
||||||
|
|
||||||
isDevMapperDevice = virIsDevMapperDevice(devpath);
|
isDevMapperDevice = virIsDevMapperDevice(devpath);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* vircgroup.c: methods for managing control cgroups
|
* vircgroup.c: methods for managing control cgroups
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010-2012 Red Hat, Inc.
|
* Copyright (C) 2010-2013 Red Hat, Inc.
|
||||||
* Copyright IBM Corp. 2008
|
* Copyright IBM Corp. 2008
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
@ -37,7 +37,6 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <libgen.h>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
#define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
#define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "dirname.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "vircommand.h"
|
#include "vircommand.h"
|
||||||
@ -1925,7 +1926,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
config_address = basename(device_path);
|
config_address = last_component(device_path);
|
||||||
if (VIR_ALLOC(*bdf) != 0) {
|
if (VIR_ALLOC(*bdf) != 0) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -56,7 +56,7 @@ typedef virStorageFileMetadata *virStorageFileMetadataPtr;
|
|||||||
struct _virStorageFileMetadata {
|
struct _virStorageFileMetadata {
|
||||||
char *backingStore; /* Canonical name (absolute file, or protocol) */
|
char *backingStore; /* Canonical name (absolute file, or protocol) */
|
||||||
char *backingStoreRaw; /* If file, original name, possibly relative */
|
char *backingStoreRaw; /* If file, original name, possibly relative */
|
||||||
char *directory; /* The directory containing basename(backingStoreRaw) */
|
char *directory; /* The directory containing basename of backingStoreRaw */
|
||||||
int backingStoreFormat; /* enum virStorageFileFormat */
|
int backingStoreFormat; /* enum virStorageFileFormat */
|
||||||
bool backingStoreIsFile;
|
bool backingStoreIsFile;
|
||||||
virStorageFileMetadataPtr backingMeta;
|
virStorageFileMetadataPtr backingMeta;
|
||||||
|
Loading…
Reference in New Issue
Block a user