1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#[cfg(feature = "fs_permission_translation")]
use std::io;
#[cfg(feature = "fs_permission_translation")]
use std::str::FromStr;
use std::time::Duration;

#[cfg(feature = "fs_permission_translation")]
use libc;
#[allow(unused_imports)]
use serde::de::Error;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde_keyvalue::FromKeyValues;

/// The caching policy that the file system should report to the FUSE client. By default the FUSE
/// protocol uses close-to-open consistency. This means that any cached contents of the file are
/// invalidated the next time that file is opened.
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, FromKeyValues)]
#[serde(rename_all = "kebab-case")]
pub enum CachePolicy {
    /// The client should never cache file data and all I/O should be directly forwarded to the
    /// server. This policy must be selected when file contents may change without the knowledge of
    /// the FUSE client (i.e., the file system does not have exclusive access to the directory).
    Never,

    /// The client is free to choose when and how to cache file data. This is the default policy
    /// and uses close-to-open consistency as described in the enum documentation.
    #[default]
    Auto,

    /// The client should always cache file data. This means that the FUSE client will not
    /// invalidate any cached data that was returned by the file system the last time the file was
    /// opened. This policy should only be selected when the file system has exclusive access to
    /// the directory.
    Always,
}

const fn config_default_timeout() -> Duration {
    Duration::from_secs(5)
}

const fn config_default_negative_timeout() -> Duration {
    Duration::ZERO
}

const fn config_default_posix_acl() -> bool {
    true
}

const fn config_default_security_ctx() -> bool {
    true
}

fn deserialize_timeout<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Duration, D::Error> {
    let secs = u64::deserialize(deserializer)?;

    Ok(Duration::from_secs(secs))
}

#[cfg(feature = "arc_quota")]
fn deserialize_privileged_quota_uids<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Vec<libc::uid_t>, D::Error> {
    // space-separated list
    let s: &str = serde::Deserialize::deserialize(deserializer)?;
    s.split(" ")
        .map(|s| {
            s.parse::<libc::uid_t>().map_err(|e| {
                <D as Deserializer>::Error::custom(format!(
                    "failed to parse priviledged quota uid {s}: {e}"
                ))
            })
        })
        .collect()
}

/// Permission structure that is configured to map the UID-GID at runtime
#[cfg(feature = "fs_permission_translation")]
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct PermissionData {
    /// UID to be set for all the files in the path inside guest.
    pub guest_uid: libc::uid_t,

    /// GID to be set for all the files in the path inside guest.
    pub guest_gid: libc::gid_t,

    /// UID to be set for all the files in the path in the host.
    pub host_uid: libc::uid_t,

    /// GID to be set for all the files in the path in the host.
    pub host_gid: libc::gid_t,

    /// umask to be set at runtime for the files in the path.
    pub umask: libc::mode_t,

    /// This is the absolute path from the root of the shared directory.
    pub perm_path: String,
}

#[cfg(feature = "fs_runtime_ugid_map")]
fn process_ugid_map(result: Vec<Vec<String>>) -> Result<Vec<PermissionData>, io::Error> {
    let mut permissions = Vec::new();

    for inner_vec in result {
        let guest_uid = match libc::uid_t::from_str(&inner_vec[0]) {
            Ok(uid) => uid,
            Err(_) => {
                return Err(io::Error::from_raw_os_error(libc::EINVAL));
            }
        };

        let guest_gid = match libc::gid_t::from_str(&inner_vec[1]) {
            Ok(gid) => gid,
            Err(_) => {
                return Err(io::Error::from_raw_os_error(libc::EINVAL));
            }
        };

        let host_uid = match libc::uid_t::from_str(&inner_vec[2]) {
            Ok(uid) => uid,
            Err(_) => {
                return Err(io::Error::from_raw_os_error(libc::EINVAL));
            }
        };

        let host_gid = match libc::gid_t::from_str(&inner_vec[3]) {
            Ok(gid) => gid,
            Err(_) => {
                return Err(io::Error::from_raw_os_error(libc::EINVAL));
            }
        };

        let umask = match libc::mode_t::from_str(&inner_vec[4]) {
            Ok(mode) => mode,
            Err(_) => {
                return Err(io::Error::from_raw_os_error(libc::EINVAL));
            }
        };

        let perm_path = inner_vec[5].clone();

        // Create PermissionData and push it to the vector
        permissions.push(PermissionData {
            guest_uid,
            guest_gid,
            host_uid,
            host_gid,
            umask,
            perm_path,
        });
    }

    Ok(permissions)
}

#[cfg(feature = "fs_runtime_ugid_map")]
fn deserialize_ugid_map<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Vec<PermissionData>, D::Error> {
    // space-separated list
    let s: &str = serde::Deserialize::deserialize(deserializer)?;

    let result: Vec<Vec<String>> = s
        .split(';')
        .map(|group| group.trim().split(' ').map(String::from).collect())
        .collect();

    // Length Validation for each inner vector
    for inner_vec in &result {
        if inner_vec.len() != 6 {
            return Err(D::Error::custom(
                "Invalid ugid_map format. Each group must have 6 elements.",
            ));
        }
    }

    let permissions = match process_ugid_map(result) {
        Ok(p) => p,
        Err(e) => {
            return Err(D::Error::custom(format!(
                "Error processing uid_gid_map: {}",
                e
            )));
        }
    };

    Ok(permissions)
}

/// Options that configure the behavior of the file system.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, FromKeyValues)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct Config {
    /// How long the FUSE client should consider directory entries and file/directory attributes to
    /// be valid.
    /// This value corresponds to `entry_timeout` and `attr_timeout` in
    /// [libfuse's `fuse_config`](https://libfuse.github.io/doxygen/structfuse__config.html), but
    /// we use the same value for the two.
    ///
    /// If the contents of a directory or the attributes of a file or directory can only be
    /// modified by the FUSE client (i.e., the file system has exclusive access), then this should
    /// be a large value.
    /// The default value for this option is 5 seconds.
    #[serde(
        default = "config_default_timeout",
        deserialize_with = "deserialize_timeout"
    )]
    pub timeout: Duration,

    /// How long the FUSE client can cache negative lookup results.
    /// If a file lookup fails, the client can assume the file doesn't exist until the timeout and
    ///  won't send lookup.
    /// The value 0 means that negative lookup shouldn't be cached.
    ///
    /// If the contents of a directory can only be modified by the FUSE client (i.e., the file
    /// system has exclusive access), then this should be a large value.
    /// The default value for this option is 0 seconds (= no negative cache).
    #[serde(
        default = "config_default_negative_timeout",
        deserialize_with = "deserialize_timeout"
    )]
    pub negative_timeout: Duration,

    /// The caching policy the file system should use. See the documentation of `CachePolicy` for
    /// more details.
    #[serde(default, alias = "cache")]
    pub cache_policy: CachePolicy,

    /// Whether the file system should enabled writeback caching. This can improve performance as
    /// it allows the FUSE client to cache and coalesce multiple writes before sending them to
    /// the file system. However, enabling this option can increase the risk of data corruption
    /// if the file contents can change without the knowledge of the FUSE client (i.e., the
    /// server does **NOT** have exclusive access). Additionally, the file system should have
    /// read access to all files in the directory it is serving as the FUSE client may send
    /// read requests even for files opened with `O_WRONLY`.
    ///
    /// Therefore callers should only enable this option when they can guarantee that: 1) the file
    /// system has exclusive access to the directory and 2) the file system has read permissions
    /// for all files in that directory.
    ///
    /// The default value for this option is `false`.
    #[serde(default)]
    pub writeback: bool,

    /// Controls whether security.* xattrs (except for security.selinux) are re-written. When this
    /// is set to true, the server will add a "user.virtiofs" prefix to xattrs in the security
    /// namespace. Setting these xattrs requires CAP_SYS_ADMIN in the namespace where the file
    /// system was mounted and since the server usually runs in an unprivileged user namespace,
    /// it's unlikely to have that capability.
    ///
    /// The default value for this option is `false`.
    #[serde(default, alias = "rewrite-security-xattrs")]
    pub rewrite_security_xattrs: bool,

    /// Use case-insensitive lookups for directory entries (ASCII only).
    ///
    /// The default value for this option is `false`.
    #[serde(default)]
    pub ascii_casefold: bool,

    // UIDs which are privileged to perform quota-related operations. We cannot perform a
    // CAP_FOWNER check so we consult this list when the VM tries to set the project quota and
    // the process uid doesn't match the owner uid. In that case, all uids in this list are
    // treated as if they have CAP_FOWNER.
    #[cfg(feature = "arc_quota")]
    #[serde(default, deserialize_with = "deserialize_privileged_quota_uids")]
    pub privileged_quota_uids: Vec<libc::uid_t>,

    /// Use DAX for shared files.
    ///
    /// Enabling DAX can improve performance for frequently accessed files by mapping regions of
    /// the file directly into the VM's memory region, allowing direct access with the cost of
    /// slightly increased latency the first time the file is accessed. Additionally, since the
    /// mapping is shared directly from the host kernel's file cache, enabling DAX can improve
    /// performance even when the cache policy is `Never`.
    ///
    /// The default value for this option is `false`.
    #[serde(default, alias = "dax")]
    pub use_dax: bool,

    /// Enable support for POSIX acls.
    ///
    /// Enable POSIX acl support for the shared directory. This requires that the underlying file
    /// system also supports POSIX acls.
    ///
    /// The default value for this option is `true`.
    #[serde(default = "config_default_posix_acl")]
    pub posix_acl: bool,

    // Maximum number of dynamic permission paths.
    //
    // The dynamic permission paths are used to set specific paths certain uid/gid after virtiofs
    // device is created. It is for arcvm special usage, normal device should not support
    // this feature.
    //
    // The default value for this option is 0.
    #[serde(default)]
    pub max_dynamic_perm: usize,

    // Maximum number of dynamic xattr paths.
    //
    // The dynamic xattr paths are used to set specific paths certain xattr after virtiofs
    // device is created. It is for arcvm special usage, normal device should not support
    // this feature.
    //
    // The default value for this option is 0.
    #[serde(default)]
    pub max_dynamic_xattr: usize,

    // Controls whether fuse_security_context feature is enabled
    //
    // The FUSE_SECURITY_CONTEXT feature needs write data into /proc/thread-self/attr/fscreate.
    // For the hosts that prohibit the write operation, the option should be set to false to
    // disable the FUSE_SECURITY_CONTEXT feature. When FUSE_SECURITY_CONTEXT is disabled, the
    // security context won't be passed with fuse request, which makes guest created files/dir
    // having unlabeled security context or empty security context.
    //
    // The default value for this option is true
    #[serde(default = "config_default_security_ctx")]
    pub security_ctx: bool,

    // Specifies run-time UID/GID mapping that works without user namespaces.
    //
    // The virtio-fs usually does mapping of UIDs/GIDs between host and guest with user namespace.
    // In Android, however, user namespace isn't available for non-root users.
    // This allows mapping UIDs and GIDs without user namespace by intercepting FUSE
    // requests and translating UID/GID in virito-fs's process at runtime.
    //
    // The format is "guest-uid, guest-gid, host-uid, host-gid, umask, path;{repeat}"
    //
    // guest-uid: UID to be set for all the files in the path inside guest.
    // guest-gid: GID to be set for all the files in the path inside guest.
    // host-uid: UID to be set for all the files in the path in the host.
    // host-gid: GID to be set for all the files in the path in the host.
    // umask: umask to be set at runtime for the files in the path.
    // path: This is the absolute path from the root of the shared directory.
    //
    // This follows similar format to ARCVM IOCTL "FS_IOC_SETPERMISSION"
    #[cfg(feature = "fs_runtime_ugid_map")]
    #[serde(default, deserialize_with = "deserialize_ugid_map")]
    pub ugid_map: Vec<PermissionData>,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            timeout: config_default_timeout(),
            negative_timeout: config_default_negative_timeout(),
            cache_policy: Default::default(),
            writeback: false,
            rewrite_security_xattrs: false,
            ascii_casefold: false,
            #[cfg(feature = "arc_quota")]
            privileged_quota_uids: Default::default(),
            use_dax: false,
            posix_acl: config_default_posix_acl(),
            max_dynamic_perm: 0,
            max_dynamic_xattr: 0,
            security_ctx: config_default_security_ctx(),
            #[cfg(feature = "fs_runtime_ugid_map")]
            ugid_map: Vec::new(),
        }
    }
}

#[cfg(all(test, feature = "fs_runtime_ugid_map"))]
mod tests {

    use super::*;
    #[test]
    fn test_deserialize_ugid_map_valid() {
        let input_string =
            "\"1000 1000 1000 1000 0022 /path/to/dir;2000 2000 2000 2000 0022 /path/to/other/dir\"";

        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer).unwrap();

        assert_eq!(result.len(), 2);
        assert_eq!(
            result,
            vec![
                PermissionData {
                    guest_uid: 1000,
                    guest_gid: 1000,
                    host_uid: 1000,
                    host_gid: 1000,
                    umask: 22,
                    perm_path: "/path/to/dir".to_string(),
                },
                PermissionData {
                    guest_uid: 2000,
                    guest_gid: 2000,
                    host_uid: 2000,
                    host_gid: 2000,
                    umask: 22,
                    perm_path: "/path/to/other/dir".to_string(),
                },
            ]
        );
    }

    #[test]
    fn test_process_ugid_map_valid() {
        let input_vec = vec![
            vec![
                "1000".to_string(),
                "1000".to_string(),
                "1000".to_string(),
                "1000".to_string(),
                "0022".to_string(),
                "/path/to/dir".to_string(),
            ],
            vec![
                "2000".to_string(),
                "2000".to_string(),
                "2000".to_string(),
                "2000".to_string(),
                "0022".to_string(),
                "/path/to/other/dir".to_string(),
            ],
        ];

        let result = process_ugid_map(input_vec).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(
            result,
            vec![
                PermissionData {
                    guest_uid: 1000,
                    guest_gid: 1000,
                    host_uid: 1000,
                    host_gid: 1000,
                    umask: 22,
                    perm_path: "/path/to/dir".to_string(),
                },
                PermissionData {
                    guest_uid: 2000,
                    guest_gid: 2000,
                    host_uid: 2000,
                    host_gid: 2000,
                    umask: 22,
                    perm_path: "/path/to/other/dir".to_string(),
                },
            ]
        );
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_format() {
        let input_string = "\"1000 1000 1000 0022 /path/to/dir\""; // Missing one element

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_guest_uid() {
        let input_string = "\"invalid 1000 1000 1000 0022 /path/to/dir\""; // Invalid guest-UID

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_guest_gid() {
        let input_string = "\"1000 invalid 1000 1000 0022 /path/to/dir\""; // Invalid guest-GID

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_umask() {
        let input_string = "\"1000 1000 1000 1000 invalid /path/to/dir\""; // Invalid umask

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_host_uid() {
        let input_string = "\"1000 1000 invalid 1000 0022 /path/to/dir\""; // Invalid host-UID

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_ugid_map_invalid_host_gid() {
        let input_string = "\"1000 1000 1000 invalid 0022 /path/to/dir\""; // Invalid host-UID

        // Create a Deserializer from the input string
        let mut deserializer = serde_json::Deserializer::from_str(input_string);
        let result = deserialize_ugid_map(&mut deserializer);
        assert!(result.is_err());
    }
}