the_permalink() is a not a return function, it echoes the permalink. Replace it with get_permalink, which returns the permalink.
if($gallery_images != ''){
foreach ($gallery_images as $gallery_image){
$thumb = wp_get_attachment_image_src($gallery_image[SN.'gallery_post_image']['id'], 'post-thumb', false);
echo '<li><a <a href="'.get_permalink().'"><img src="'.$thumb[0].'" alt="'.$gallery_image[SN.'gallery_post_title'].'" /></a><p class="flex-caption">'.$gallery_image[SN.'gallery_post_title'].'</p></li>';
}
}