Example #1
0
/**
 * Parse /proc/%d/cmdline file
 * @param filepath Path to file ~ use preallocated buffer for the path
 * @param buffer output buffer with non-zero size
 * @return ps-like command info or NULL
 */
static inline bool get_process_cmdline(const char* filepath, struct oscap_buffer* const buffer){

	int fd = open(filepath, O_RDONLY, 0);

	if (fd < 0) {
		return false;
	}

	oscap_buffer_clear(buffer);



	for(;;) {
		static const int chunk_size = 1024;
		char chunk[chunk_size];
		// Read data, store to buffer
		ssize_t read_size = read(fd, chunk, chunk_size );
		if (read_size < 0) {
			close(fd);
			return false;
		}
		oscap_buffer_append_binary_data(buffer, chunk, read_size);

		// If reach end of file, then end the loop
		if (chunk_size != read_size) {
			break;
		}
	}

	close(fd);

	int length = oscap_buffer_get_length(buffer);
	char* buffer_mem = oscap_buffer_get_raw(buffer);

	if ( length == 0 ) { // empty file
		return false;
	} else {

		// Skip multiple trailing zeros
		int i = length - 1;
		while ( (i > 0) && (buffer_mem[i] == '\0') ) {
			--i;
		}

		// Program and args are separated by '\0'
		// Replace them with spaces ' '
		while( i >= 0 ){
			char chr = buffer_mem[i];
			if ( ( chr == '\0') || ( chr == '\n' ) ) {
				buffer_mem[i] = ' ';
			} else if ( !isprint(chr) ) { // "ps" replace non-printable characters with '.' (LC_ALL=C)
				buffer_mem[i] = '.';
			}
			--i;
		}
	}
	return true;
}
Example #2
0
int test_null_parameters() {
	struct oscap_buffer *s = oscap_buffer_new();
	oscap_buffer_append_binary_data(s, NULL, 4);

	if (oscap_buffer_get_length(s) != 0) {
		fprintf(stderr, "Appended NULL changed the length of string\n");
		return 1;
	}
	oscap_buffer_free(NULL);
	oscap_buffer_free(s);
	return 0;
}
Example #3
0
int test_append_binary_data()
{
	static const char* data[] = {
		"\0",
		"\0A",
		"ABC"
	};

	struct oscap_buffer *s = oscap_buffer_new();
	ASSERT_LENGTH(s, 0);

	oscap_buffer_append_binary_data(s, data[0], 1);
	ASSERT_LENGTH(s, 1);

	oscap_buffer_append_binary_data(s, data[1], 2);
	ASSERT_LENGTH(s, 3);

	oscap_buffer_append_binary_data(s, data[0], 0); // do not increase length
	ASSERT_LENGTH(s, 3);

	oscap_buffer_append_binary_data(s, data[2], 3);
	ASSERT_LENGTH(s, 6);


	// Check output data
	int length = oscap_buffer_get_length(s); // length is valid due to previous ASSERT_LENGTH
	static const char expected_result[] = {'\0', '\0', 'A', 'A', 'B', 'C'};
	const char* result = oscap_buffer_get_raw(s);
	for (int i = 0; i < length; ++i) {
		char expected = expected_result[i];
		char got = result[i];
		if ( expected != got ) {
			fprintf(stderr, "Invalid character at index %d, expected %x and got %x\n", i, expected, got);
			return 1;
		}
	}

	return 0;
}