Esempio n. 1
0
/* Get the section offsets from a package file, offsets will be malloc'd to
 * the appropriate size and the caller must free() them */
static int module_package_read_offsets(sepol_module_package_t *mod,
				       struct policy_file *file,
				       size_t **offsets,
				       uint32_t *sections)
{
	uint32_t *buf, nsec;
	unsigned i;

	buf = next_entry(file, sizeof(uint32_t) * 3);
	if (!buf) {
		ERR(file->handle, "module package header truncated");
		return -1;
	}
	if (le32_to_cpu(buf[0]) != SEPOL_MODULE_PACKAGE_MAGIC) {
		ERR(file->handle, "wrong magic number for module package:  expected %u, got %u", SEPOL_MODULE_PACKAGE_MAGIC, le32_to_cpu(buf[0]));
		return -1;	
	}
	
	mod->version = le32_to_cpu(buf[1]);
	nsec = *sections = le32_to_cpu(buf[2]);

	if (nsec > MAXSECTIONS) {
		ERR(file->handle, "too many sections (%u) in module package", nsec);
		return -1;
	}

	*offsets = (size_t *)malloc((nsec + 1) * sizeof(size_t));
	if (!*offsets) {
		ERR(file->handle, "out of memory");
		return -1;
	}

	buf = next_entry(file, sizeof(uint32_t) * nsec);
	if (!buf) {
		ERR(file->handle, "module package offset array truncated");
		return -1;
	}

	for (i = 0; i < nsec; i++) {
		(*offsets)[i] = le32_to_cpu(buf[i]);
		if (i && (*offsets)[i] < (*offsets)[i - 1]) {
			ERR(file->handle, "offsets are not increasing (at %u, "
				"offset %zu -> %zu", i, (*offsets)[i-1], (*offsets)[i]);
			return -1;
		}
	}

	(*offsets)[nsec] = policy_file_length(file);
	return 0;
}
Esempio n. 2
0
/* Get the section offsets from a package file, offsets will be malloc'd to
 * the appropriate size and the caller must free() them */
static int module_package_read_offsets(sepol_module_package_t * mod,
				       struct policy_file *file,
				       size_t ** offsets, uint32_t * sections)
{
	uint32_t *buf = NULL, nsec;
	unsigned i;
	size_t *off = NULL;
	int rc;

	buf = malloc(sizeof(uint32_t)*3);
	if (!buf) {
		ERR(file->handle, "out of memory");
		goto err;
	}
	  
	rc = next_entry(buf, file, sizeof(uint32_t) * 3);
	if (rc < 0) {
		ERR(file->handle, "module package header truncated");
		goto err;
	}
	if (le32_to_cpu(buf[0]) != SEPOL_MODULE_PACKAGE_MAGIC) {
		ERR(file->handle,
		    "wrong magic number for module package:  expected %#08x, got %#08x",
		    SEPOL_MODULE_PACKAGE_MAGIC, le32_to_cpu(buf[0]));
		goto err;
	}

	mod->version = le32_to_cpu(buf[1]);
	nsec = *sections = le32_to_cpu(buf[2]);

	if (nsec > MAXSECTIONS) {
		ERR(file->handle, "too many sections (%u) in module package",
		    nsec);
		goto err;
	}

	off = (size_t *) malloc((nsec + 1) * sizeof(size_t));
	if (!off) {
		ERR(file->handle, "out of memory");
		goto err;
	}

	free(buf);
	buf = malloc(sizeof(uint32_t) * nsec);
	if (!buf) {
		ERR(file->handle, "out of memory");
		goto err;
	}
	rc = next_entry(buf, file, sizeof(uint32_t) * nsec);
	if (rc < 0) {
		ERR(file->handle, "module package offset array truncated");
		goto err;
	}

	for (i = 0; i < nsec; i++) {
		off[i] = le32_to_cpu(buf[i]);
		if (i && off[i] < off[i - 1]) {
			ERR(file->handle, "offsets are not increasing (at %u, "
			    "offset %zu -> %zu", i, off[i - 1],
			    off[i]);
			goto err;
		}
	}

	rc = policy_file_length(file, &off[nsec]);
	if (rc < 0)
		goto err;

	if (nsec && off[nsec] < off[nsec-1]) {
		ERR(file->handle, "offset greater than file size (at %u, "
		    "offset %zu -> %zu", nsec, off[nsec - 1],
		    off[nsec]);
		goto err;
	}
	*offsets = off;
	free(buf);
	return 0;

err:
	free(buf);
	free(off);
	return -1;
}