Blob Blame Raw
From fa9b2cd01fb5dbe583f5063f611a45c9d033a54a Mon Sep 17 00:00:00 2001
From: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
Date: Tue, 30 Sep 2014 16:19:47 +0300
Subject: [PATCH 1/1] perl: Don't call detach after failed perl_parse

Don't call "detach" callback in rlm_perl, if perl_parse of the Perl
module failed.

This fixes segfault when the module file cannot be read:

    Can't open perl script "/etc/raddb/mods-config/perl/example.pl": Permission denied
    rlm_perl: perl_parse failed: /etc/raddb/mods-config/perl/example.pl not found or has syntax errors.
    /etc/raddb/mods-enabled/perl[7]: Instantiation failed for module "perl"
    Segmentation fault
---
 src/modules/rlm_perl/rlm_perl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/modules/rlm_perl/rlm_perl.c b/src/modules/rlm_perl/rlm_perl.c
index d423524..039d7c8 100644
--- a/src/modules/rlm_perl/rlm_perl.c
+++ b/src/modules/rlm_perl/rlm_perl.c
@@ -73,6 +73,7 @@ typedef struct rlm_perl_t {
 	char const	*xlat_name;
 	char const	*perl_flags;
 	PerlInterpreter	*perl;
+	bool             perl_parsed;
 	pthread_key_t	*thread_key;
 
 #ifdef USE_ITHREADS
@@ -538,6 +539,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
 	PL_endav = (AV *)NULL;
 
 	if(!exitstatus) {
+		inst->perl_parsed = true;
 		perl_run(inst->perl);
 	} else {
 		ERROR("rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
@@ -1012,7 +1014,7 @@ static int mod_detach(void *instance)
 	}
 #endif
 
-	if (inst->func_detach) {
+	if (inst->perl_parsed && inst->func_detach) {
 		dTHXa(inst->perl);
 		PERL_SET_CONTEXT(inst->perl);
 		{
-- 
2.1.0