From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 24 Dec 2009 07:33:04 +0300 From: Alexey Tourbin To: ALT Devel discussion list Message-ID: <20091224043304.GF9864@altlinux.org> Mail-Followup-To: ALT Devel discussion list References: <20091217215921.GO9864@altlinux.org> <20091217224429.GA28428@wo.int.altlinux.org> <20091218000542.GQ9864@altlinux.org> <20091219002640.GU9864@altlinux.org> <20091219195007.GA1460@wo.int.altlinux.org> <20091219210349.GX9864@altlinux.org> <20091219214217.GA2033@wo.int.altlinux.org> <20091219235738.GB9864@altlinux.org> <20091221200852.GB14299@wo.int.altlinux.org> <20091224005722.GE9864@altlinux.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="oHeY95fWM4VAmTwS" Content-Disposition: inline In-Reply-To: <20091224005722.GE9864@altlinux.org> Subject: Re: [devel] verify-elf lint=normal (elflint) X-BeenThere: devel@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: ALT Linux Team development discussions List-Id: ALT Linux Team development discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Dec 2009 04:33:05 -0000 Archived-At: List-Archive: List-Post: --oHeY95fWM4VAmTwS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Dec 24, 2009 at 03:57:22AM +0300, Alexey Tourbin wrote: > On Mon, Dec 21, 2009 at 11:08:52PM +0300, Dmitry V. Levin wrote: > > glibc-6:2.10.1-alt8 > > loadable segment [1] is writable but contains no writable sections > > verify-elf: WARNING: ./sbin/glibc_preinstall: eu-elflint failed >=20 > > klibc-1.5.15-alt4 > > loadable segment [3] is writable but contains no writable sections > > verify-elf: WARNING: ./lib/mkinitrd/klibc/bin/cat: eu-elflint failed > > [9 lines skipped] > > verify-elf: WARNING: ./lib/mkinitrd/klibc/bin/umount: eu-elflint failed > > loadable segment [3] is writable but contains no writable sections > > verify-elf: WARNING: ./usr/lib/klibc/bin/cat: eu-elflint failed > > [12 lines skipped] > > section [ 4] '.interp' present in object file > > section [ 4] '.interp' has SHF_ALLOC flag set but there is no loadable = segment > > verify-elf: WARNING: ./usr/lib/klibc/lib/interp.o: eu-elflint failed >=20 > > util-linux-2.16.1-alt3 > > loadable segment [1] is writable but contains no writable sections > > verify-elf: WARNING: ./sbin/nologin: eu-elflint failed > > verify-elf: WARNING: ./usr/bin/pause: eu-elflint failed >=20 > It looks like all klibc executables (compiled with klcc) are subject > to this warning. Let's try to fix it before we discuss whether > lint=3Dnormal should be on by default. Let's test a simple program to examine the problem. $ cat test.c int foo =3D 0; int main() { return 0; } $ klcc test.c && readelf --segments a.out && eu-elflint --gnu-ld a.out Elf file type is EXEC (Executable file) Entry point 0x4000e8 There are 3 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 0x00000000000001ac 0x00000000000001ac R E 200000 LOAD 0x00000000000001b0 0x00000000006001b0 0x00000000006001b0 0x0000000000000000 0x0000000000000030 RW 200000 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 Section to Segment mapping: Segment Sections... 00 .text=20 01 .bss=20 02 =20 loadable segment [1] is writable but contains no writable sections $=20 The last line, which is eu-elflint output, suggests there's a problem. Now, if we replace "foo =3D 0" with "foo =3D 1", the test will pass. $ cat test.c int foo =3D 1; int main() { return 0; } $ klcc test.c && readelf --segments a.out && eu-elflint --gnu-ld a.out Elf file type is EXEC (Executable file) Entry point 0x4000e8 There are 3 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 0x00000000000001ac 0x00000000000001ac R E 200000 LOAD 0x00000000000001ac 0x00000000006001ac 0x00000000006001ac 0x0000000000000004 0x000000000000002c RW 200000 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RWE 8 Section to Segment mapping: Segment Sections... 00 .text=20 01 .data .bss=20 02 =20 No errors $ (The last line now says "No errors".) So the problem is that, while ".data" section contributes to the ELF segment being writable, ".bss" section alone does not. Let's see the code. elfutils-0.143/src/elflint.c: 3694 if (ehdr->e_type !=3D ET_REL && (shdr->sh_flags & SHF_ALLOC) = !=3D 0) 3695 { 3696 /* Make sure the section is contained in a loaded segment 3697 and that the initialization part matches NOBITS sectio= ns. */ 3698 int pcnt; 3699 GElf_Phdr phdr_mem; 3700 GElf_Phdr *phdr; 3701 =20 3702 for (pcnt =3D 0; pcnt < ehdr->e_phnum; ++pcnt) 3703 if ((phdr =3D gelf_getphdr (ebl->elf, pcnt, &phdr_mem))= !=3D NULL 3704 && ((phdr->p_type =3D=3D PT_LOAD 3705 && (shdr->sh_flags & SHF_TLS) =3D=3D 0) 3706 || (phdr->p_type =3D=3D PT_TLS 3707 && (shdr->sh_flags & SHF_TLS) !=3D 0)) 3708 && phdr->p_offset <=3D shdr->sh_offset 3709 && (phdr->p_offset + phdr->p_filesz > shdr->sh_offs= et 3710 || (phdr->p_offset + phdr->p_memsz > shdr->sh_o= ffset 3711 && shdr->sh_type =3D=3D SHT_NOBITS))) 3712 { 3713 /* Found the segment. */ 3714 if (phdr->p_offset + phdr->p_memsz 3715 < shdr->sh_offset + shdr->sh_size) 3716 ERROR (gettext ("\ 3717 section [%2zu] '%s' not fully contained in segment of program heade= r entry %d\n"), 3718 cnt, section_name (ebl, cnt), pcnt); 3719 =20 3720 if (shdr->sh_type =3D=3D SHT_NOBITS) 3721 { 3722 if (shdr->sh_offset < phdr->p_offset + phdr->p_= filesz 3723 && !is_debuginfo) 3724 ERROR (gettext ("\ 3725 section [%2zu] '%s' has type NOBITS but is read from the file in se= gment of program header entry %d\n"), 3726 cnt, section_name (ebl, cnt), pcnt); 3727 } 3728 else 3729 { 3730 const GElf_Off end =3D phdr->p_offset + phdr->p= _filesz; 3731 if (shdr->sh_offset > end || 3732 (shdr->sh_offset =3D=3D end && shdr->sh_siz= e !=3D 0)) 3733 ERROR (gettext ("\ 3734 section [%2zu] '%s' has not type NOBITS but is not read from the fi= le in segment of program header entry %d\n"), 3735 cnt, section_name (ebl, cnt), pcnt); 3736 } 3737 =20 3738 if (shdr->sh_type !=3D SHT_NOBITS) 3739 { 3740 if ((shdr->sh_flags & SHF_EXECINSTR) !=3D 0) 3741 { 3742 segment_flags[pcnt] |=3D PF_X; 3743 if ((phdr->p_flags & PF_X) =3D=3D 0) 3744 ERROR (gettext ("\ 3745 section [%2zu] '%s' is executable in nonexecutable segment %d\n"), 3746 cnt, section_name (ebl, cnt), pcnt= ); 3747 } 3748 =20 3749 if ((shdr->sh_flags & SHF_WRITE) !=3D 0) 3750 { 3751 segment_flags[pcnt] |=3D PF_W; 3752 if (0 /* XXX vdso images have this */ 3753 && (phdr->p_flags & PF_W) =3D=3D 0) 3754 ERROR (gettext ("\ 3755 section [%2zu] '%s' is writable in unwritable segment %d\n"), 3756 cnt, section_name (ebl, cnt), pcnt= ); 3757 } 3758 } This code tests each allocatable section (3694) and, among other things, sets segments_flags (3742, 3751) -- permissions which corresponding ELF segment should have (the segments are tested later against segments_flags). However, only NOBITS sections contribute to ELF segment flags (3738). This is possibly wrong: ".bss" section, despite the fact that it is NOBITS (filled with zeroes upon startup), should be writable. Here's a dumb patch which fixes the problem. --- src/elflint.c- 2009-12-24 01:09:24 +0000 +++ src/elflint.c 2009-12-24 03:48:23 +0000 @@ -3794,7 +3794,10 @@ section [%2zu] '%s' has not type NOBITS=20 section [%2zu] '%s' is executable in nonexecutable segment %d\n"), cnt, section_name (ebl, cnt), pcnt); } + } =20 + if (shdr->sh_type !=3D SHT_NOBITS || strcmp (scnname, ".bss") =3D=3D 0) + { if ((shdr->sh_flags & SHF_WRITE) !=3D 0) { segment_flags[pcnt] |=3D PF_W; --oHeY95fWM4VAmTwS Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAksy7wAACgkQfBKgtDjnu0bG/gCgqOEAKyPAz+Voyopx4ZuGe2x+ GZMAn1OKq7Q5t6Qtp5VSG3RBDQl76BA4 =eA+x -----END PGP SIGNATURE----- --oHeY95fWM4VAmTwS--