mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
To: <ntfs3@lists.linux.dev>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	<linux-fsdevel@vger.kernel.org>
Subject: [PATCH 05/10] fs/ntfs3: Do not update primary boot in ntfs_init_from_boot()
Date: Mon, 8 May 2023 16:38:09 +0400	[thread overview]
Message-ID: <45f18870-379f-fa88-382c-5600e31d33a6@paragon-software.com> (raw)
In-Reply-To: <b21a4bc9-166d-2631-d73b-cb4e802ff69e@paragon-software.com>

'cause it may be faked boot.
Let ntfs to be mounted and update boot later.

Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
---
  fs/ntfs3/super.c | 58 ++++++++++++++++++++++++++++++++----------------
  1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index ecf899d571d8..2b48b45238ea 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -711,9 +711,16 @@ static u32 true_sectors_per_clst(const struct 
NTFS_BOOT *boot)

  /*
   * ntfs_init_from_boot - Init internal info from on-disk boot sector.
+ *
+ * NTFS mount begins from boot - special formatted 512 bytes.
+ * There are two boots: the first and the last 512 bytes of volume.
+ * The content of boot is not changed during ntfs life.
+ *
+ * NOTE: ntfs.sys checks only first (primary) boot.
+ * chkdsk checks both boots.
   */
  static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
-                   u64 dev_size)
+                   u64 dev_size, struct NTFS_BOOT **boot2)
  {
      struct ntfs_sb_info *sbi = sb->s_fs_info;
      int err;
@@ -937,23 +944,11 @@ static int ntfs_init_from_boot(struct super_block 
*sb, u32 sector_size,

      if (bh->b_blocknr && !sb_rdonly(sb)) {
          /*
-          * Alternative boot is ok but primary is not ok.
-          * Update primary boot.
-         */
-        struct buffer_head *bh0 = sb_getblk(sb, 0);
-        if (bh0) {
-            if (buffer_locked(bh0))
-                __wait_on_buffer(bh0);
-
-            lock_buffer(bh0);
-            memcpy(bh0->b_data, boot, sizeof(*boot));
-            set_buffer_uptodate(bh0);
-            mark_buffer_dirty(bh0);
-            unlock_buffer(bh0);
-            if (!sync_dirty_buffer(bh0))
-                ntfs_warn(sb, "primary boot is updated");
-            put_bh(bh0);
-        }
+         * Alternative boot is ok but primary is not ok.
+         * Do not update primary boot here 'cause it may be faked boot.
+         * Let ntfs to be mounted and update boot later.
+         */
+        *boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN);
      }

  out:
@@ -1000,6 +995,7 @@ static int ntfs_fill_super(struct super_block *sb, 
struct fs_context *fc)
      u16 *shared;
      struct MFT_REF ref;
      bool ro = sb_rdonly(sb);
+    struct NTFS_BOOT *boot2 = NULL;

      ref.high = 0;

@@ -1030,7 +1026,7 @@ static int ntfs_fill_super(struct super_block *sb, 
struct fs_context *fc)

      /* Parse boot. */
      err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
-                  bdev_nr_bytes(bdev));
+                  bdev_nr_bytes(bdev), &boot2);
      if (err)
          goto out;

@@ -1412,6 +1408,29 @@ static int ntfs_fill_super(struct super_block 
*sb, struct fs_context *fc)
          goto put_inode_out;
      }

+    if (boot2) {
+        /*
+         * Alternative boot is ok but primary is not ok.
+         * Volume is recognized as NTFS. Update primary boot.
+         */
+        struct buffer_head *bh0 = sb_getblk(sb, 0);
+        if (bh0) {
+            if (buffer_locked(bh0))
+                __wait_on_buffer(bh0);
+
+            lock_buffer(bh0);
+            memcpy(bh0->b_data, boot2, sizeof(*boot2));
+            set_buffer_uptodate(bh0);
+            mark_buffer_dirty(bh0);
+            unlock_buffer(bh0);
+            if (!sync_dirty_buffer(bh0))
+                ntfs_warn(sb, "primary boot is updated");
+            put_bh(bh0);
+        }
+
+        kfree(boot2);
+    }
+
      return 0;

  put_inode_out:
@@ -1424,6 +1443,7 @@ static int ntfs_fill_super(struct super_block *sb, 
struct fs_context *fc)
      put_mount_options(sbi->options);
      put_ntfs(sbi);
      sb->s_fs_info = NULL;
+    kfree(boot2);

      return err;
  }
-- 
2.34.1


  parent reply	other threads:[~2023-05-08 12:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-08 12:35 [PATCH 00/10] Refactoring and bugfix Konstantin Komarov
2023-05-08 12:36 ` [PATCH 01/10] fs/ntfs3: Correct checking while generating attr_list Konstantin Komarov
2023-05-08 12:36 ` [PATCH 02/10] fs/ntfs3: Fix ntfs_atomic_open Konstantin Komarov
2023-05-08 12:36 ` [PATCH 03/10] fs/ntfs3: Mark ntfs dirty when on-disk struct is corrupted Konstantin Komarov
2023-05-08 12:37 ` [PATCH 04/10] fs/ntfs3: Alternative boot if primary boot " Konstantin Komarov
2023-05-08 12:38 ` Konstantin Komarov [this message]
2023-05-08 12:38 ` [PATCH 06/10] fs/ntfs3: Code formatting Konstantin Komarov
2023-05-08 12:38 ` [PATCH 07/10] fs/ntfs3: Code refactoring Konstantin Komarov
2023-05-08 12:39 ` [PATCH 08/10] fs/ntfs3: Add ability to format new mft records with bigger/smaller header Konstantin Komarov
2023-05-08 12:39 ` [PATCH 09/10] fs/ntfs3: Fix endian problem Konstantin Komarov
2023-05-08 12:40 ` [PATCH 10/10] fs/ntfs3: Add support /proc/fs/ntfs3/<dev>/volinfo and /proc/fs/ntfs3/<dev>/label Konstantin Komarov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45f18870-379f-fa88-382c-5600e31d33a6@paragon-software.com \
    --to=almaz.alexandrovich@paragon-software.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntfs3@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®