Merge "libcutils: ashmem fortify and comply with Android coding standard"

This commit is contained in:
Mark Salyzyn 2016-02-02 21:31:01 +00:00 committed by Gerrit Code Review
commit 0e0a4628f6
2 changed files with 41 additions and 31 deletions

View file

@ -20,17 +20,19 @@
* used by the simulator. * used by the simulator.
*/ */
#include <unistd.h> #include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/ashmem.h> #include <linux/ashmem.h>
#include <cutils/ashmem.h> #include <cutils/ashmem.h>
#define ASHMEM_DEVICE "/dev/ashmem" #define ASHMEM_DEVICE "/dev/ashmem"
/* /*
* ashmem_create_region - creates a new ashmem region and returns the file * ashmem_create_region - creates a new ashmem region and returns the file
@ -41,50 +43,55 @@
*/ */
int ashmem_create_region(const char *name, size_t size) int ashmem_create_region(const char *name, size_t size)
{ {
int fd, ret; int ret, save_errno;
fd = open(ASHMEM_DEVICE, O_RDWR); int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR));
if (fd < 0) if (fd < 0) {
return fd; return fd;
}
if (name) { if (name) {
char buf[ASHMEM_NAME_LEN] = {0}; char buf[ASHMEM_NAME_LEN] = {0};
strlcpy(buf, name, sizeof(buf)); strlcpy(buf, name, sizeof(buf));
ret = ioctl(fd, ASHMEM_SET_NAME, buf); ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
if (ret < 0) if (ret < 0) {
goto error; goto error;
} }
}
ret = ioctl(fd, ASHMEM_SET_SIZE, size); ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
if (ret < 0) if (ret < 0) {
goto error; goto error;
}
return fd; return fd;
error: error:
close(fd); save_errno = errno;
return ret; close(fd);
errno = save_errno;
return ret;
} }
int ashmem_set_prot_region(int fd, int prot) int ashmem_set_prot_region(int fd, int prot)
{ {
return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
} }
int ashmem_pin_region(int fd, size_t offset, size_t len) int ashmem_pin_region(int fd, size_t offset, size_t len)
{ {
struct ashmem_pin pin = { offset, len }; struct ashmem_pin pin = { offset, len };
return ioctl(fd, ASHMEM_PIN, &pin); return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
} }
int ashmem_unpin_region(int fd, size_t offset, size_t len) int ashmem_unpin_region(int fd, size_t offset, size_t len)
{ {
struct ashmem_pin pin = { offset, len }; struct ashmem_pin pin = { offset, len };
return ioctl(fd, ASHMEM_UNPIN, &pin); return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
} }
int ashmem_get_size_region(int fd) int ashmem_get_size_region(int fd)
{ {
return ioctl(fd, ASHMEM_GET_SIZE, NULL); return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
} }

View file

@ -78,8 +78,11 @@ int ashmem_get_size_region(int fd)
return -1; return -1;
} }
// Check if this is an "ashmem" region. /*
// TODO: This is very hacky, and can easily break. We need some reliable indicator. * Check if this is an "ashmem" region.
* TODO: This is very hacky, and can easily break.
* We need some reliable indicator.
*/
if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) { if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
errno = ENOTTY; errno = ENOTTY;
return -1; return -1;