Re: libspf2 crash

Ladar Levison <[email protected]> Tue, 10 Jun 2008 00:39:23 -0500
Newsgroups gmane.mail.spam.spf.devel
Message-ID <[email protected]>
If you execute the attached code right now (Tuesday, 12:30am) you'll be able to 
reproduce the crash. Note that these spammer domains disappear often, so I'm not 
sure how long this will last. Also note I'm loading the library dynamically, so 
update the code accordingly. The Valgrind output is:

==22263== Conditional jump or move depends on uninitialised value(s)
==22263==    at 0x555E8E: __ns_name_skip (in /lib/libresolv-2.3.4.so)
==22263==    by 0x54F2DB: __dn_skipname (in /lib/libresolv-2.3.4.so)
==22263==    by 0x40166BF: __ns_skiprr (__ns_initparse.c:83)
==22263==    by 0x4016839: __ns_initparse (__ns_initparse.c:123)
==22263==    by 0x400F3D4: SPF_dns_resolv_lookup (spf_dns_resolv.c:188)
==22263==    by 0x400E5E6: SPF_dns_lookup (spf_dns.c:114)
==22263==    by 0x4013513: SPF_record_interpret (spf_interpret.c:778)
==22263==    by 0x4015289: SPF_request_query_record (spf_request.c:224)
==22263==    by 0x401530B: SPF_request_query_mailfrom (spf_request.c:255)
==22263==    by 0x8048623: main (main.c:134)

I'll continue to investigate, and if I come up with a patch, I'll post it.
main.c (text/plain, 4.4 KB)
// LibC
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <pthread.h>
#include <dlfcn.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <semaphore.h>
#include <netdb.h>
#include <signal.h>
#include <execinfo.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <spf.h>

void *lavalib = NULL;

void (*SPF_server_free_d)(SPF_server_t *sp) = NULL;
SPF_errcode_t (*SPF_request_set_ipv4_str_d)(SPF_request_t *sr, const char *astr) = NULL;
SPF_request_t * (*SPF_request_new_d)(SPF_server_t *spf_server) = NULL;
SPF_server_t * (*SPF_server_new_d)(SPF_server_dnstype_t dnstype, int debug) = NULL;
SPF_errcode_t (*SPF_request_set_helo_dom_d)(SPF_request_t *sr, const char *dom) = NULL;
int (*SPF_request_set_env_from_d)(SPF_request_t *sr, const char *from) = NULL;
SPF_errcode_t (*SPF_request_query_mailfrom_d)(SPF_request_t *spf_request, SPF_response_t **spf_responsep) = NULL;
SPF_result_t (*SPF_response_result_d)(SPF_response_t *rp) = NULL;
void (*SPF_response_free_d)(SPF_response_t *rp) = NULL;
void (*SPF_request_free_d)(SPF_request_t *sr) = NULL;
void (*SPF_get_lib_version_d)(int *major, int *minor, int *patch) = NULL;

short load_function_pointers(unsigned number, char *names[], void **pointers[]) {

	unsigned increment;
	
	if (number == 0 || pointers == NULL || names == NULL) {
		printf("An invalid request was made.");
		return 0;
	}
	
	if (lavalib == NULL) {
		printf("The lava library pointer was NULL. Unable to load function pointers.");
		return 0;
	}
	
	// Loop through and setup the function pointers.
	for (increment = 0; increment < number; increment++) {
		if ((*(pointers[increment]) = dlsym(lavalib, names[increment])) == NULL) {
			printf("Unable to establish a pointer to the function %s.", names[increment]);
			return 0;
		}
	}
	
	return 1;
}


int load_symbols_spf(void) {
	
	char *names[] = { "SPF_server_free", "SPF_server_new", "SPF_request_new", "SPF_request_set_ipv4_str", "SPF_request_set_helo_dom",
		"SPF_request_set_env_from", "SPF_request_query_mailfrom", "SPF_response_result", "SPF_response_free", "SPF_request_free",
		"SPF_get_lib_version" };
	void **pointers[] = { (void *)&SPF_server_free_d, (void *)&SPF_server_new_d, (void *)&SPF_request_new_d, (void *)&SPF_request_set_ipv4_str_d,
		(void *)&SPF_request_set_helo_dom_d, (void *)&SPF_request_set_env_from_d, (void *)&SPF_request_query_mailfrom_d, (void *)&SPF_response_result_d,
		(void *)&SPF_response_free_d, (void *)&SPF_request_free_d, (void *)&SPF_get_lib_version_d };
 
	if (sizeof(pointers) / sizeof(void *) != sizeof(names) / sizeof(char *)) {
		printf("The number of names doesn't match the number of pointers.");
		return 0;
	}
	
	if (load_function_pointers(sizeof(pointers) / sizeof(void *), names, pointers) != 1) {
		return 0;
	}
	
	return 1;
}

int main() {

	short sucess = 0;	
	SPF_server_t *object = NULL;
	SPF_request_t *spf_request = NULL;
	SPF_response_t *spf_response = NULL;
	char *ip = "67.203.31.103", *helo = "mx103.citylinenews.com", *mailfrom = "[email protected]";
	
	if ((lavalib = dlopen("/home/ladar/Projects/lavad/library/libspf2-1.2.5/src/libspf2/.libs/libspf2.so", RTLD_NOW | RTLD_LOCAL)) == NULL) {
		printf("Could not open lavad.lib.");
		return 0;
	}
	
	load_symbols_spf();
	
	// Init.
	if ((object = SPF_server_new_d(SPF_DNS_RESOLV, 0)) == NULL) {
		printf("Could not initialize SPF object.");
		return 0;
	}
	
	while (1) {
	
		// Request context.
		if ((spf_request = SPF_request_new_d(object)) == NULL) {
			printf("Could not setup the SPF object.");
			return 0;
		}
		
		// Set the IP.
		if (SPF_request_set_ipv4_str_d(spf_request, ip) != SPF_E_SUCCESS) {
			printf("Could not setup the IP.");
			return 0;
		}
	
		// Set the helo name.
		if (SPF_request_set_helo_dom_d(spf_request, helo) != SPF_E_SUCCESS) {
			printf("Could not setup the helo name.");
			return 0;
		}
		
		// Set the envelope from.
		if (SPF_request_set_env_from_d(spf_request, mailfrom) != SPF_E_SUCCESS) {
			printf("Could not setup the mailfrom.");
			return 0;
		}
			
		// Perform the query.
		if (SPF_request_query_mailfrom_d(spf_request, &spf_response) == SPF_E_SUCCESS) {
			sucess = 1;
		}
		
		if (spf_response != NULL) {
			SPF_response_free_d(spf_response);
			spf_response = NULL;
		}
		
		if (spf_request != NULL) {
			SPF_request_free_d(spf_request);
			spf_request = NULL;
		}
		
		sleep(1);
	}
		
	SPF_server_free_d(object);
	return 0;
}