From owner-svn-src-stable@freebsd.org Tue Mar 17 19:53:04 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8E0F02711CE; Tue, 17 Mar 2020 19:53:04 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48hkNw0NWXz4d61; Tue, 17 Mar 2020 19:53:04 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id DD8D7E806; Tue, 17 Mar 2020 19:53:03 +0000 (UTC) (envelope-from emaste@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 02HJr3TC096053; Tue, 17 Mar 2020 19:53:03 GMT (envelope-from emaste@FreeBSD.org) Received: (from emaste@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 02HJr3oT096051; Tue, 17 Mar 2020 19:53:03 GMT (envelope-from emaste@FreeBSD.org) Message-Id: <202003171953.02HJr3oT096051@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: emaste set sender to emaste@FreeBSD.org using -f From: Ed Maste Date: Tue, 17 Mar 2020 19:53:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r359049 - in stable/12: tools/tools/controlelf usr.bin usr.bin/elfctl X-SVN-Group: stable-12 X-SVN-Commit-Author: emaste X-SVN-Commit-Paths: in stable/12: tools/tools/controlelf usr.bin usr.bin/elfctl X-SVN-Commit-Revision: 359049 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Mar 2020 19:53:04 -0000 Author: emaste Date: Tue Mar 17 19:53:03 2020 New Revision: 359049 URL: https://svnweb.freebsd.org/changeset/base/359049 Log: MFC r358512: Move ELF feature note tool to usr.bin/elfctl elfctl is a tool for modifying the NT_FREEBSD_FEATURE_CTL ELF note, which contains a set of flags for enabling or disabling vulnerability mitigations and other features. Also merge follow-on commits: r358518 elfctl: initialize features r358546 elfctl: tiny style(9) cleanup, use bool where appropriate r358622 elfctl: style(9): use C99 uintX_t types r358623 elfctl: check read return value r358889 elfctl: remove memory leak Sponsored by: The FreeBSD Foundation Added: stable/12/usr.bin/elfctl/ - copied from r358512, head/usr.bin/elfctl/ Deleted: stable/12/tools/tools/controlelf/ Modified: stable/12/usr.bin/Makefile stable/12/usr.bin/elfctl/elfctl.c Directory Properties: stable/12/ (props changed) Modified: stable/12/usr.bin/Makefile ============================================================================== --- stable/12/usr.bin/Makefile Tue Mar 17 19:20:12 2020 (r359048) +++ stable/12/usr.bin/Makefile Tue Mar 17 19:53:03 2020 (r359049) @@ -33,6 +33,7 @@ SUBDIR= alias \ dirname \ du \ elf2aout \ + elfctl \ elfdump \ enigma \ env \ Modified: stable/12/usr.bin/elfctl/elfctl.c ============================================================================== --- head/usr.bin/elfctl/elfctl.c Mon Mar 2 02:36:41 2020 (r358512) +++ stable/12/usr.bin/elfctl/elfctl.c Tue Mar 17 19:53:03 2020 (r359049) @@ -48,9 +48,9 @@ __FBSDID("$FreeBSD$"); -static bool convert_to_feature_val(char *, u_int32_t *); +static bool convert_to_feature_val(char *, uint32_t *); static bool edit_file_features(Elf *, int, int, char *); -static bool get_file_features(Elf *, int, int, u_int32_t *, u_int64_t *); +static bool get_file_features(Elf *, int, int, uint32_t *, uint64_t *); static void print_features(void); static bool print_file_features(Elf *, int, int, char *); static void usage(void); @@ -85,13 +85,14 @@ main(int argc, char **argv) GElf_Ehdr ehdr; Elf *elf; Elf_Kind kind; - int ch, fd, editfeatures, retval; + int ch, fd, retval; char *features; - bool lflag; + bool editfeatures, lflag; lflag = 0; - editfeatures = 0; + editfeatures = false; retval = 0; + features = NULL; if (elf_version(EV_CURRENT) == EV_NONE) errx(EXIT_FAILURE, "elf_version error"); @@ -104,7 +105,7 @@ main(int argc, char **argv) break; case 'e': features = optarg; - editfeatures = 1; + editfeatures = true; break; case 'h': default: @@ -205,11 +206,11 @@ usage(void) } static bool -convert_to_feature_val(char *feature_str, u_int32_t *feature_val) +convert_to_feature_val(char *feature_str, uint32_t *feature_val) { char *feature; int i, len; - u_int32_t input; + uint32_t input; char operation; input = 0; @@ -246,8 +247,8 @@ convert_to_feature_val(char *feature_str, u_int32_t *f static bool edit_file_features(Elf *elf, int phcount, int fd, char *val) { - u_int32_t features; - u_int64_t off; + uint32_t features; + uint64_t off; if (!get_file_features(elf, phcount, fd, &features, &off)) { warnx("NT_FREEBSD_FEATURE_CTL note not found"); @@ -280,7 +281,7 @@ print_features(void) static bool print_file_features(Elf *elf, int phcount, int fd, char *filename) { - u_int32_t features; + uint32_t features; unsigned long i; if (!get_file_features(elf, phcount, fd, &features, NULL)) { @@ -301,15 +302,14 @@ print_file_features(Elf *elf, int phcount, int fd, cha } static bool -get_file_features(Elf *elf, int phcount, int fd, u_int32_t *features, - u_int64_t *off) +get_file_features(Elf *elf, int phcount, int fd, uint32_t *features, + uint64_t *off) { GElf_Phdr phdr; Elf_Note note; unsigned long read_total; int namesz, descsz, i; char *name; - ssize_t size; /* * Go through each program header to find one that is of type PT_NOTE @@ -331,9 +331,9 @@ get_file_features(Elf *elf, int phcount, int fd, u_int read_total = 0; while (read_total < phdr.p_filesz) { - size = read(fd, ¬e, sizeof(note)); - if (size < (ssize_t)sizeof(note)) { - warn("read() failed:"); + if (read(fd, ¬e, sizeof(note)) < + (ssize_t)sizeof(note)) { + warnx("elf note header too short"); return (false); } read_total += sizeof(note); @@ -349,7 +349,11 @@ get_file_features(Elf *elf, int phcount, int fd, u_int return (false); } descsz = roundup2(note.n_descsz, 4); - size = read(fd, name, namesz); + if (read(fd, name, namesz) < namesz) { + warnx("elf note name too short"); + free(name); + return (false); + } read_total += namesz; if (note.n_namesz != 8 || @@ -366,7 +370,7 @@ get_file_features(Elf *elf, int phcount, int fd, u_int continue; } - if (note.n_descsz < sizeof(u_int32_t)) { + if (note.n_descsz < sizeof(uint32_t)) { warnx("Feature descriptor can't " "be less than 4 bytes"); free(name); @@ -377,9 +381,14 @@ get_file_features(Elf *elf, int phcount, int fd, u_int * XXX: For now we look at only 4 bytes of the * descriptor. This should respect descsz. */ - if (note.n_descsz > sizeof(u_int32_t)) + if (note.n_descsz > sizeof(uint32_t)) warnx("Feature note is bigger than expected"); - read(fd, features, sizeof(u_int32_t)); + if (read(fd, features, sizeof(uint32_t)) < + (ssize_t)sizeof(uint32_t)) { + warnx("feature note data too short"); + free(name); + return (false); + } if (off != NULL) *off = phdr.p_offset + read_total; free(name);