Port the check permission patch from master