From a13a0530dc243ffa8ba7fb08ffb6305946396387 Mon Sep 17 00:00:00 2001 From: Alexandr Nedvedicky Date: Sun, 21 Apr 2024 09:41:58 +0200 Subject: [PATCH] fix crash in ecp_nistz256_point_add_affine() The .rodata section with precomputed constant `ecp_nistz256_precomputed` needs to be terminated by .text, because the ecp_nistz256_precomputed' happens to be the first section in the file. The lack of .text makes code to arrive into the same .rodata section where ecp_nistz256_precomputed is found. The exception is raised as soon as CPU attempts to execute the code from read only section. Fixes #24184 --- crypto/perlasm/x86_64-xlate.pl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/crypto/perlasm/x86_64-xlate.pl b/crypto/perlasm/x86_64-xlate.pl index 80fe1ff1977a79..69bf613c05781f 100755 --- a/crypto/perlasm/x86_64-xlate.pl +++ b/crypto/perlasm/x86_64-xlate.pl @@ -909,21 +909,38 @@ # to deal with nasm/masm assembly. # $self->{value} =~ s/(.+)\s+align\s*=.*$/$1/; + $current_segment = pop(@segment_stack); + if (not $current_segment) { + # if no previous section is defined, then assume .text + # so code does not land in .data section by accident. + # this deals with inconsistency of perl-assembly files. + push(@segment_stack, ".text"); + } # # $$line may still contains align= option. We do care # about section type here. # $current_segment = $$line; $current_segment =~ s/([^\s]+).*$/$1/; + push(@segment_stack, $current_segment); if (!$elf && $current_segment eq ".rodata") { if ($flavour eq "macosx") { $self->{value} = ".section\t__DATA,__const"; } + elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.rodata"; } } if (!$elf && $current_segment eq ".init") { if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; } elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; } } } elsif ($dir =~ /\.(text|data)/) { + $current_segment = pop(@segment_stack); + if (not $current_segment) { + # if no previous section is defined, then assume .text + # so code does not land in .data section by accident. + # this deals with inconsistency of perl-assembly files. + push(@segment_stack, ".text"); + } $current_segment=".$1"; + push(@segment_stack, $current_segment); } elsif ($dir =~ /\.hidden/) { if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$$line"; } elsif ($flavour eq "mingw64") { $self->{value} = ""; } @@ -931,7 +948,15 @@ $self->{value} = "$dir\t$prefix$$line"; $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx"); } elsif ($dir =~ /\.previous/) { - $self->{value} = "" if ($flavour eq "mingw64"); + pop(@segment_stack); #pop ourselves + $current_segment = pop(@segment_stack); #pop previous section + if (not $current_segment) { + # if no previous segment was defined assume .text so + # the code does not accidentally land in .data section. + $current_segment = ".text" + } + push(@segment_stack, $current_segment); + $self->{value} = $current_segment if ($flavour eq "mingw64"); } $$line = ""; return $self;