Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp27871319rwd; Tue, 4 Jul 2023 09:02:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6pl9Xe5fTmaCjEj+8WvX5Eh0VmzczlTpkPe+FUMfNkEZ3T0J/7fAv/eUOYYL8/hfzMhgTp X-Received: by 2002:a05:6a20:12cf:b0:112:cf5:d5cc with SMTP id v15-20020a056a2012cf00b001120cf5d5ccmr20685641pzg.1.1688486526141; Tue, 04 Jul 2023 09:02:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688486526; cv=none; d=google.com; s=arc-20160816; b=FJUPi904GCkFAOTXeYslnUtiZNbtQYKxEm7+6eeGmI53TVJKsyzloZq1W39vlyZ+la a3cVXHxwuWBoS9yLkLhJEJnFHbUN6qqx3fMt2YbMV73/IGE44ZXd2IRcN1zkylfJeJiR +RBEXZLU3nxodpFLBqdHFZNO4u/OU6ola1Cqt8mF8JMJ6NpcgTL/ygSWPBOu+tfnDrI/ nqLtfx3/fFOF1zAl+qlT4+Ivv9Ok0lwbZOqXSS4JOgt+hX06Oft+HNz75jrRMGq0bq5Q K8U8TrZQHSDb/dph94gX+k16DnCd+8ZTRm6swiTVxnCrog7I8CMlLgA0/EalLecmRFHv U/OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:content-transfer-encoding :content-id:mime-version:subject:cc:to:references:in-reply-to:from :organization:dkim-signature; bh=we0f1CDdulhi3OjoFmt4741QCm67FVAPa8fyIc7peJE=; fh=51wms6boE4GvjmosLq9DnAPh/Ah3fB0FVI5yu8bsZ8M=; b=LZiAwJy2nG7y33Vj62P62GHdYWJfvg9ZpJPW9HgLbNjbuAhKuAZkv/RhllGpYjrfnk xsGuq8ZcIdTq8zbq5Xc1UKeE7meLqtF49qqtYSgxC5NhtUIovj7bMddAGbQR+ljnjXJX XHh4Q/v/qBMHeumtGIVfFIB4DGZuEE5i6B282LPbtzkEq5UOP4A+0Dt3RwGv5klQ7hq3 6pbcPlD+1Xz8JLDASOs/0hS7FhCoUnq7JBxf6Xp4YutNg5wDmGC5KnVweF/dX+calCW/ APFyMMRyQx174iq69d5xI67+vb9Jddh61XMxoAQ9c8t3TzPu622JRrT5iZkv+W8S0gpO 3u0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GesL3hPy; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a63f508000000b00553a56f7b36si22381437pgh.522.2023.07.04.09.01.45; Tue, 04 Jul 2023 09:02:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GesL3hPy; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231328AbjGDPyG (ORCPT + 99 others); Tue, 4 Jul 2023 11:54:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231341AbjGDPyE (ORCPT ); Tue, 4 Jul 2023 11:54:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2888A10C1 for ; Tue, 4 Jul 2023 08:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688485996; 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=we0f1CDdulhi3OjoFmt4741QCm67FVAPa8fyIc7peJE=; b=GesL3hPyoqCuTgpz0aC9Cvd5WZHsx8JpIy7mq48OEVdViux10X0W1PAb3AIy5hc3uCzk2o nz95tbQrIzH3Yjox+f2zvMeC51RbE7YuRMCIQbzix05gPblJYpBtmgkz1iuthutT//RRUz FOzHHRXBam+IwXo0SwkV95VZNMtrTE8= 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-538-I4ZJUzz0PE2EiXGsamv06Q-1; Tue, 04 Jul 2023 11:53:14 -0400 X-MC-Unique: I4ZJUzz0PE2EiXGsamv06Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EA5B2185A795; Tue, 4 Jul 2023 15:53:13 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.195]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CA32C00049; Tue, 4 Jul 2023 15:53:13 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: References: To: =?UTF-8?B?T25kcmVqIE1vc27DocSNZWs=?= Cc: dhowells@redhat.com, Linux Crypto Mailing List , Herbert Xu , Paolo Abeni , netdev@vger.kernel.org Subject: Re: Regression bisected to "crypto: af_alg: Convert af_alg_sendpage() to use MSG_SPLICE_PAGES" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1585769.1688485992.1@warthog.procyon.org.uk> Content-Transfer-Encoding: quoted-printable Date: Tue, 04 Jul 2023 16:53:12 +0100 Message-ID: <1585770.1688485992@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Here's a smaller test program. If it works correctly, it will print: 000: 5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a 020: 5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a ... With the merging problem, it will print: 000: 5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a 020: 5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a 040: 6162636465666768696a6b6c6d6e6f707172737475767778797a313233343536 060: 4142434445464748494a4b4c4d4e4f505152535455565758595a313233343536 ... You can see the data from the send() ("abcd...") got appended to the page passed by vmsplice() ("5a" x 64). Arguably, if vmsplice() is given SPLICE_F_GIFT, AF_ALG would be within its rights to remove the page from the caller's address space (as fuse will do with SPLICE_F_MOVE), attach it to its scatterlist and append data to it. = In such a case, however, the calling process should no longer be able to acce= ss the page - and in the case of libkcapi, the heap would be corrupted. David --- // SPDX-License-Identifier: GPL-2.0-or-later /* AF_ALG vmsplice test * * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #define OSERROR(X, Y) \ do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); } } while (0) #define min(x, y) ((x) < (y) ? (x) : (y)) static unsigned char buffer[4096 * 32] __attribute__((aligned(4096))); static unsigned char iv[16]; static unsigned char key[16]; static const struct sockaddr_alg sa =3D { .salg_family =3D AF_ALG, .salg_type =3D "skcipher", .salg_name =3D "cbc(aes)", }; static void algif_add_set_op(struct msghdr *msg, unsigned int op) { struct cmsghdr *__cmsg; __cmsg =3D msg->msg_control + msg->msg_controllen; __cmsg->cmsg_len =3D CMSG_LEN(sizeof(unsigned int)); __cmsg->cmsg_level =3D SOL_ALG; __cmsg->cmsg_type =3D ALG_SET_OP; *(unsigned int *)CMSG_DATA(__cmsg) =3D op; msg->msg_controllen +=3D CMSG_ALIGN(__cmsg->cmsg_len); } static void algif_add_set_iv(struct msghdr *msg, const void *iv, size_t iv= len) { struct af_alg_iv *ivbuf; struct cmsghdr *__cmsg; __cmsg =3D msg->msg_control + msg->msg_controllen; __cmsg->cmsg_len =3D CMSG_LEN(sizeof(*ivbuf) + ivlen); __cmsg->cmsg_level =3D SOL_ALG; __cmsg->cmsg_type =3D ALG_SET_IV; ivbuf =3D (struct af_alg_iv *)CMSG_DATA(__cmsg); ivbuf->ivlen =3D ivlen; memcpy(ivbuf->iv, iv, ivlen); msg->msg_controllen +=3D CMSG_ALIGN(__cmsg->cmsg_len); } void check(const unsigned char *p) { unsigned int i, j; int blank =3D 0; for (i =3D 0; i < 4096; i +=3D 32) { for (j =3D 0; j < 32; j++) if (p[i + j]) break; if (j =3D=3D 32) { if (!blank) { printf("...\n"); blank =3D 1; } continue; } printf("%03x: ", i); for (j =3D 0; j < 32; j++) printf("%02x", p[i + j]); printf("\n"); blank =3D 0; } } int main(int argc, char *argv[]) { struct iovec iov; struct msghdr msg; unsigned char ctrl[4096]; ssize_t ret; size_t total =3D 0, i, out =3D 160; unsigned char *buf; int alg, sock, pfd[2]; alg =3D socket(AF_ALG, SOCK_SEQPACKET, 0); OSERROR(alg, "AF_ALG"); OSERROR(bind(alg, (struct sockaddr *)&sa, sizeof(sa)), "bind"); OSERROR(setsockopt(alg, SOL_ALG, ALG_SET_KEY, key, sizeof(key)), "ALG_SET_KEY"); sock =3D accept(alg, NULL, 0); OSERROR(sock, "accept"); memset(&msg, 0, sizeof(msg)); msg.msg_control =3D ctrl; algif_add_set_op(&msg, ALG_OP_ENCRYPT); algif_add_set_iv(&msg, iv, sizeof(iv)); OSERROR(sendmsg(sock, &msg, MSG_MORE), "sock/sendmsg"); OSERROR(pipe(pfd), "pipe"); buf =3D mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0= ); OSERROR(buf, "mmap"); memset(buf, 0x5a, 64); iov.iov_base =3D buf; iov.iov_len =3D 64; OSERROR(vmsplice(pfd[1], &iov, 1, SPLICE_F_MORE), "vmsplice"); OSERROR(splice(pfd[0], NULL, sock, NULL, 64, SPLICE_F_MORE), "splice"); OSERROR(send(sock, "abcdefghijklmnopqrstuvwxyz123456ABCDEFGHIJKLMNOPQRSTU= VWXYZ123456", 64, 0), "sock/send"); while (total > 0) { ret =3D read(sock, buffer, min(sizeof(buffer), total)); OSERROR(ret, "sock/read"); if (ret =3D=3D 0) break; total -=3D ret; if (out > 0) { ret =3D min(out, ret); out -=3D ret; for (i =3D 0; i < ret; i++) printf("%02x", (unsigned char)buffer[i]); } printf("...\n"); } check(buf); OSERROR(close(sock), "sock/close"); OSERROR(close(alg), "alg/close"); OSERROR(close(pfd[0]), "close/p0"); OSERROR(close(pfd[1]), "close/p1"); return 0; }