Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6113214ybi; Wed, 31 Jul 2019 08:32:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqxkHz7T/A+OFFsqjnCY71u8zt7XI0VD276fu7hrs+03idmBAL9KgRxYIMHJgEIB/7QcM6v6 X-Received: by 2002:a17:90a:9bca:: with SMTP id b10mr3550293pjw.90.1564587169927; Wed, 31 Jul 2019 08:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564587169; cv=none; d=google.com; s=arc-20160816; b=w75kFzlUojl0kNhIz+Nvc8kn2iUzr433ddgG3tb5hoabC928Vy2WP0RnTffv6QBQAP lysAWDZZe82XbGnuHIAJDsyGBwkeN8dgkARJUGXbJEXpTvE2ucXJRgzTZIFchhy7NPjc hKgXyOF08ZQZTsvCPzS9O8B5FXz0LSD+HM+24rlqGkTDFY38wmG/AS8OxSrFbMDjmYCx U1nd4rtUfYz0UupJV+sHxsL2hs/CfsjabpQwZ/SNnPh5JVQu1fy0egshy+NAqxADOMPk dc9xVYRDcAxHjy34ZNzVSKoP5+zMbGTkW8TKj5l572gD3y0wbSjGp2kZvJLt8U3Qnz9K MJAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rpVydprU+qUIbG2ieS0/pcejmTH1ILSgd/PE5xIU2l8=; b=d67yOLBpa5+gneg+KeKb9Jhd8DRIPedgzk/REtEGhOHGI0bZut6MlmGhQxUBWaCUtD JE1mV7eYvb7+L4mUTXCrrobVY91CQjgxw0PACaKGCaZ3tvahV9ddWox5R+ijsXfGaTqy Dn7D+yj6KoCEC/gWve6C+BtTM8UtMDU0kn5e1M0kXZLPX6FuKMAS0v8T3NhTqdu1WV7Z /ajsTFPfYXlAFtJbwIfIWFzrPM0jtMBmmVkHiEVWoY5+pwmXhQ/aq8hhHjKssVpK2oyA h7eWb36nFiWD0K50SfQnWpGU81u2s+fTobUBVCac1baMV0tBaEGZDW2btslpUmkuzCzv iqww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="DRp4/04Q"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a94si1647845pje.19.2019.07.31.08.32.34; Wed, 31 Jul 2019 08:32:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="DRp4/04Q"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727942AbfGaPXu (ORCPT + 99 others); Wed, 31 Jul 2019 11:23:50 -0400 Received: from mail-ed1-f49.google.com ([209.85.208.49]:33129 "EHLO mail-ed1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727004AbfGaPXt (ORCPT ); Wed, 31 Jul 2019 11:23:49 -0400 Received: by mail-ed1-f49.google.com with SMTP id i11so2570759edq.0 for ; Wed, 31 Jul 2019 08:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rpVydprU+qUIbG2ieS0/pcejmTH1ILSgd/PE5xIU2l8=; b=DRp4/04Ql9tuwZ8HmklcrVGbpbwpqyiHWrb5OFmDW/SEC8peDq2WPBaUHFkAudB5fO O6QRunFSo/ysHy81uSqyDDbaDB0RbNas5DoSFJjERVndASSmEQlNkxI6m4UzvLb1lYXe HR9PxFKm/WpkDGRuHh7/II3kWjLslUMYjW15KfsW1uUXxZKw3qH1ykdFZqetYNR65uFr gcHNHUE6tyTdsEhVT4TE8EFJvr35k771sHCQxS/OJ1ZA0dTfkaCGA+sR1YMCZ/ySG35J zgO0k7b/GKJH49Q3m+NOFpVJ9izt7DqzZczYOnuYmoEesdA/OGp8B5vhT4m926rqes+k RWuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rpVydprU+qUIbG2ieS0/pcejmTH1ILSgd/PE5xIU2l8=; b=W6XPhwb6MbUeFAHJSC2rJAEy1sPIJWvMgnyaF5Zj08p1+GiWQhlyOvpNUPpRBH9kLb sdIxGvragIG8xd/9jPH1Enpj/xXmfm1dhExn9pf0W4dkao0rn4GC2BJT+JYT0pDGHOso UG9kRNql4yOHfVNpIEMszKwhhFn2pwZVHc/Ej2lP2ucMlwcw0fZHFex2HuLrUnPJNkww 43U/KuxmWWo3bZiBnCehNd9wHpYjCa0VTCsWem9+0CGn5/3lNwnm85ZXHyfKsdBr14aF HNQvrC2kSjGh6f5C+o/WjcZWBJbRpTqUWTfg7+7YbzC/GLuem03TRGsgaf9AP8C7FFmg bz2A== X-Gm-Message-State: APjAAAUgUcHzhAP+FEUpR2j0P2xQdEql/EYMITinWjye364FFRQDLN9h xMnGLMHFs0xUqPsPob+8WcE= X-Received: by 2002:aa7:ca45:: with SMTP id j5mr106898585edt.217.1564586627658; Wed, 31 Jul 2019 08:23:47 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id j12sm12429043ejd.30.2019.07.31.08.23.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:23:47 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 957D31048AB; Wed, 31 Jul 2019 18:08:17 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCHv2 59/59] x86/mktme: Demonstration program using the MKTME APIs Date: Wed, 31 Jul 2019 18:08:13 +0300 Message-Id: <20190731150813.26289-60-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alison Schofield Signed-off-by: Alison Schofield Signed-off-by: Kirill A. Shutemov --- Documentation/x86/mktme/index.rst | 1 + Documentation/x86/mktme/mktme_demo.rst | 53 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Documentation/x86/mktme/mktme_demo.rst diff --git a/Documentation/x86/mktme/index.rst b/Documentation/x86/mktme/index.rst index ca3c76adc596..3af322d13225 100644 --- a/Documentation/x86/mktme/index.rst +++ b/Documentation/x86/mktme/index.rst @@ -10,3 +10,4 @@ Multi-Key Total Memory Encryption (MKTME) mktme_configuration mktme_keys mktme_encrypt + mktme_demo diff --git a/Documentation/x86/mktme/mktme_demo.rst b/Documentation/x86/mktme/mktme_demo.rst new file mode 100644 index 000000000000..5af78617f887 --- /dev/null +++ b/Documentation/x86/mktme/mktme_demo.rst @@ -0,0 +1,53 @@ +Demonstration Program using MKTME API's +======================================= + +/* Compile with the keyutils library: cc -o mdemo mdemo.c -lkeyutils */ + +#include +#include +#include +#include +#include +#include +#include + +#define PAGE_SIZE sysconf(_SC_PAGE_SIZE) +#define sys_encrypt_mprotect 434 + +void main(void) +{ + char *options_CPU = "algorithm=aes-xts-128 type=cpu"; + long size = PAGE_SIZE; + key_serial_t key; + void *ptra; + int ret; + + /* Allocate an MKTME Key */ + key = add_key("mktme", "testkey", options_CPU, strlen(options_CPU), + KEY_SPEC_THREAD_KEYRING); + + if (key == -1) { + printf("addkey FAILED\n"); + return; + } + /* Map a page of ANONYMOUS memory */ + ptra = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (!ptra) { + printf("failed to mmap"); + goto inval_key; + } + /* Encrypt that page of memory with the MKTME Key */ + ret = syscall(sys_encrypt_mprotect, ptra, size, PROT_NONE, key); + if (ret) + printf("mprotect error [%d]\n", ret); + + /* Enjoy that page of encrypted memory */ + + /* Free the memory */ + ret = munmap(ptra, size); + +inval_key: + /* Free the Key */ + if (keyctl(KEYCTL_INVALIDATE, key) == -1) + printf("invalidate failed on key [%d]\n", key); +} -- 2.21.0