Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include "config.h" |
| 4 | |
| 5 | extern "C" { |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 6 | #include "mboxd.h" |
Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 7 | #include "protocol.h" |
| 8 | #include "vpnor/protocol.h" |
| 9 | } |
| 10 | |
| 11 | #include "vpnor/pnor_partition_table.hpp" |
| 12 | |
| 13 | /* XXX: Maybe this should be a method on a class? */ |
| 14 | static bool vpnor_partition_is_readonly(const pnor_partition &part) |
| 15 | { |
| 16 | return part.data.user.data[1] & PARTITION_READONLY; |
| 17 | } |
| 18 | |
| 19 | typedef int (*create_window_fn)(struct mbox_context *context, |
| 20 | struct protocol_create_window *io); |
| 21 | |
| 22 | static int generic_vpnor_create_window(struct mbox_context *context, |
| 23 | struct protocol_create_window *io, |
| 24 | create_window_fn create_window) |
| 25 | { |
| 26 | if (io->req.ro) |
| 27 | { |
| 28 | return create_window(context, io); |
| 29 | } |
| 30 | |
| 31 | /* Only allow write windows on regions mapped by the ToC as writeable */ |
| 32 | size_t offset = io->req.offset; |
| 33 | offset <<= context->block_size_shift; |
| 34 | try |
| 35 | { |
| 36 | const pnor_partition &part = context->vpnor->table->partition(offset); |
| 37 | if (vpnor_partition_is_readonly(part)) |
| 38 | { |
| 39 | return -EPERM; |
| 40 | } |
| 41 | } |
| 42 | catch (const openpower::virtual_pnor::UnmappedOffset &e) |
| 43 | { |
| 44 | /* |
| 45 | * Writes to unmapped areas are not meaningful, so deny the request. |
| 46 | * This removes the ability for a compromised host to abuse unused |
| 47 | * space if any data was to be persisted (which it isn't). |
| 48 | */ |
| 49 | return -EACCES; |
| 50 | } |
| 51 | |
| 52 | return create_window(context, io); |
| 53 | } |
| 54 | |
| 55 | int protocol_v1_vpnor_create_window(struct mbox_context *context, |
| 56 | struct protocol_create_window *io) |
| 57 | { |
| 58 | return generic_vpnor_create_window(context, io, protocol_v1_create_window); |
| 59 | } |
| 60 | |
| 61 | int protocol_v2_vpnor_create_window(struct mbox_context *context, |
| 62 | struct protocol_create_window *io) |
| 63 | { |
| 64 | return generic_vpnor_create_window(context, io, protocol_v2_create_window); |
| 65 | } |