From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7B9613858D35 for ; Wed, 28 Jun 2023 19:17:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B9613858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687979831; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UPNd1ED9OZN38jTZbP7fKhKjX4Cq49aacvoVmAaHHHc=; b=ESO9+yxo/hPRAMFZHCQIxj/rqFVnRRi6W/1LVja0lhhrot2nrTiqxPFRDx6AC79uLQaE7f pPJvjj0BuPj2oufS+RDGdOZF6e0gdd6zxu0OSm+24XunI3IRayMrguVjhNhd3XKLl8GQo/ S4kntVn5eOx2gFf/kHHMZzW8lsQrkrg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-645-fJX5O47vNAyem9ubKT1sMg-1; Wed, 28 Jun 2023 15:17:07 -0400 X-MC-Unique: fJX5O47vNAyem9ubKT1sMg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E7F0C8E4680; Wed, 28 Jun 2023 19:17:06 +0000 (UTC) Received: from oak (unknown [10.22.8.210]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C717848FB01; Wed, 28 Jun 2023 19:17:06 +0000 (UTC) Date: Wed, 28 Jun 2023 15:17:05 -0400 From: Joe Simmons-Talbott To: Noah Goldstein Cc: Florian Weimer , Noah Goldstein via Libc-alpha Subject: Re: [PATCH v6 1/3] x86_64: Set the syscall register right before doing the syscall. Message-ID: <20230628191705.GQ6392@oak> References: <20230424150353.1469397-1-josimmon@redhat.com> <20230424150353.1469397-2-josimmon@redhat.com> <20230525180743.GN176347@oak> <877csvk1zt.fsf@oldenburg.str.redhat.com> <20230526125947.GP176347@oak> <87sfbeunxo.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE,URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, May 31, 2023 at 01:23:44PM -0500, Noah Goldstein wrote: > On Tue, May 30, 2023 at 5:13 AM Florian Weimer wrote: > > > > * Noah Goldstein: > > > > > On Fri, May 26, 2023 at 5:59 AM Joe Simmons-Talbott wrote: > > >> > > >> On Fri, May 26, 2023 at 09:04:06AM +0200, Florian Weimer wrote: > > >> > * Noah Goldstein via Libc-alpha: > > >> > > > >> > > I'm minorly opposed to this patch. Even if GLIBC guarantees all > > >> > > syscalls will set the number the instruction before, that's no guarantee > > >> > > for the entire program. Furthermore in the event of: > > >> > > `movl $VAL, %eax; syscall` > > >> > > It's still not safe to *always* assume that `VAL` correspond to the > > >> > > syscall number as a jump (direct or indirect) could still go between > > >> > > the instructions (i.e there is no guarantee in the assembly that the > > >> > > `mov` dominates the `syscall). > > >> > > So at the end of the day, we are bloating the library without, AFAICT, > > >> > > providing any real guarantee. Maybe I'm missing something? > > >> > > > >> > Joe, is there a size change to libc.so.6 as the result of this change? > > >> > > >> No, the size is the same with and with out this patchset on x86_64. > > >> > > > There aren't many syscalls so it's only a minor cost (hence the only > > > minor opposition), but I don't see the value this provides given that it > > > still won't be safe to assume the syscall number is always set the > > > instruction beforehand for any robust purpose. So it still feels like > > > why take any cost at all? > > > > I think there is any run-time cost at all, only the increased source > > complexity. > > > > It's much easier to change glibc than to add full register tracking to a > > the static analysis tool that discovers system calls in the disassembly. > > > > Is the aim only to verify libc.so or to verify arbitrary binaries? If the > former then sure I guess we know we only use the syscall wrapper > so this may help (more on that), but if it's arbitrary binaries there > is no guarantee they are using the GLIBC syscall wrapper for their > syscalls. > > If it really is just GLIBC then this change seems unnecessary. Even > if there can be separation between setting rax and the syscall, the > way we have the wrappers setup we know there will always be a > dominating write to rax with the syscall number so would rather see > the case where that isn't trivial to find as a motivator first. Or we could > just do source code level analysis as we will always have the > syscall number in macro invocation. > Right now we are only concerned with libc.so. Here's an example case from my installed libc.so. 000000000003ec70 <__GI___arc4random_buf.part.0>: 3ec70: 55 push %rbp 3ec71: 41 b8 3e 01 00 00 mov $0x13e,%r8d 3ec77: 48 89 e5 mov %rsp,%rbp 3ec7a: 41 56 push %r14 3ec7c: 41 55 push %r13 3ec7e: 41 54 push %r12 3ec80: 49 89 fc mov %rdi,%r12 3ec83: 53 push %rbx 3ec84: 48 89 f3 mov %rsi,%rbx 3ec87: 48 83 ec 10 sub $0x10,%rsp 3ec8b: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 3ec92: 00 00 3ec94: 48 89 45 d8 mov %rax,-0x28(%rbp) 3ec98: 31 c0 xor %eax,%eax 3ec9a: 31 d2 xor %edx,%edx 3ec9c: 48 89 de mov %rbx,%rsi 3ec9f: 4c 89 e7 mov %r12,%rdi 3eca2: 44 89 c0 mov %r8d,%eax 3eca5: 0f 05 syscall And with my patchset: 0000000000039480 <__GI___arc4random_buf.part.0>: 39480: 41 55 push %r13 39482: 41 54 push %r12 39484: 55 push %rbp 39485: 48 89 fd mov %rdi,%rbp 39488: 53 push %rbx 39489: 48 89 f3 mov %rsi,%rbx 3948c: 48 83 ec 18 sub $0x18,%rsp 39490: 31 d2 xor %edx,%edx 39492: 48 89 de mov %rbx,%rsi 39495: 48 89 ef mov %rbp,%rdi 39498: b8 3e 01 00 00 mov $0x13e,%eax 3949d: 0f 05 syscall Thanks, Joe